Dataset Viewer
Auto-converted to Parquet Duplicate
data_source
stringclasses
1 value
prompt
listlengths
2
2
ability
stringclasses
1 value
reward_model
dict
extra_info
dict
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given two integers $x$ and $y$.\n\nOutput two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 100$) β€” the number of test cases.\n\nThe only line of each test case contains two space-separated integers $x$ and $y$ ($0 \\leq x, y \\leq 9$).\n\nOutput Format:\nFor each test case, output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.\n\nTags:\n['implementation', 'sortings']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "10\n1 9\n8 4\n1 4\n3 4\n2 0\n2 4\n6 9\n3 3\n0 0\n9 9", "metadata": { "func_name": null }, "output": "1 9\n4 8\n1 4\n3 4\n0 2\n2 4\n6 9\n3 3\n0 0\n9 9", "testtype": "stdin" } ], "style": "rule" }
{ "index": 0, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given a string $s$ consisting of lowercase English letters.\n\nRearrange the characters of $s$ to form a new string $r$ that is not equal to $s$, or report that it's impossible.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) β€” the number of test cases.\n\nThe only line of each test case contains a string $s$ of length at most $10$ consisting of lowercase English letters.\n\nOutput Format:\nFor each test case, if no such string $r$ exists as described in the statement, output \"NO\" (without quotes).\n\nOtherwise, output \"YES\" (without quotes). Then, output one line β€” the string $r$, consisting of letters of string $s$.\n\nYou can output \"YES\" and \"NO\" in any case (for example, strings \"yEs\", \"yes\", and \"Yes\" will be recognized as a positive response).\n\nIf multiple answers are possible, you can output any of them.\n\nTags:\n['implementation', 'strings']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "8\ncodeforces\naaaaa\nxxxxy\nco\nd\nnutdealer\nmwistht\nhhhhhhhhhh", "metadata": { "func_name": null }, "output": "YES\nforcodesec\nNO\nYES\nxxyxx\nYES\noc\nNO\nYES\nundertale\nYES\nthtsiwm\nNO", "testtype": "stdin" } ], "style": "rule" }
{ "index": 1, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThere is a clock labeled with the numbers $1$ through $12$ in clockwise order, as shown below.\n\n![](CDN_BASE_URL/d18290022594db5fbc2eb2c94222b5d4)\n\nIn this example, $(a,b,c,d)=(2,9,10,6)$, and the strings intersect.\n\nAlice and Bob have four distinct integers $a$, $b$, $c$, $d$ not more than $12$. Alice ties a red string connecting $a$ and $b$, and Bob ties a blue string connecting $c$ and $d$. Do the strings intersect? (The strings are straight line segments.)\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 5940$) β€” the number of test cases.\n\nThe only line of each test case contains four distinct integers $a$, $b$, $c$, $d$ ($1 \\leq a, b, c, d \\leq 12$).\n\nOutput Format:\nFor each test case, output \"YES\" (without quotes) if the strings intersect, and \"NO\" (without quotes) otherwise.\n\nYou can output \"YES\" and \"NO\" in any case (for example, strings \"yEs\", \"yes\", and \"Yes\" will be recognized as a positive response).\n\nTags:\n['implementation']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "15\n2 9 10 6\n3 8 9 1\n1 2 3 4\n5 3 4 12\n1 8 2 10\n3 12 11 8\n9 10 12 1\n12 1 10 2\n3 12 6 9\n1 9 8 4\n6 7 9 12\n7 12 9 6\n10 12 11 1\n3 9 6 12\n1 4 3 5", "metadata": { "func_name": null }, "output": "YES\nNO\nNO\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES", "testtype": "stdin" } ], "style": "rule" }
{ "index": 2, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given a binary string$^{\\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.\n\n![](CDN_BASE_URL/f49d41e3510eec9591123a65ee9389ab)\n\nNote that:\n\n * each character must lie in exactly one of the pieces; * the pieces must be contiguous substrings of the original string; * you must use all the pieces in the rearrangement. \n\n$^{\\dagger}$ A binary string is a string consisting of characters $\\texttt{0}$ and $\\texttt{1}$. A sorted binary string is a binary string such that all characters $\\texttt{0}$ come before all characters $\\texttt{1}$.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 500$) β€” the number of test cases.\n\nThe only line of each test case contains a single string $s$ ($1 \\leq |s| \\leq 500$) consisting of characters $\\texttt{0}$ and $\\texttt{1}$, where $|s|$ denotes the length of the string $s$.\n\nOutput Format:\nFor each test case, output a single integer β€” the minimum number of pieces needed to be able to rearrange the string into a sorted binary string.\n\nTags:\n['dp', 'greedy', 'implementation', 'sortings', 'strings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n11010\n00000000\n1\n10\n0001111\n0110", "metadata": { "func_name": null }, "output": "3\n1\n1\n2\n1\n2", "testtype": "stdin" } ], "style": "rule" }
{ "index": 3, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nTimur is in a car traveling on the number line from point $0$ to point $n$. The car starts moving from point $0$ at minute $0$.\n\nThere are $k+1$ signs on the line at points $0, a_1, a_2, \\dots, a_k$, and Timur knows that the car will arrive there at minutes $0, b_1, b_2, \\dots, b_k$, respectively. The sequences $a$ and $b$ are strictly increasing with $a_k = n$.\n\n![](CDN_BASE_URL/bacc58f5d5013f5922e96a1311487b97)\n\nBetween any two adjacent signs, the car travels with a constant speed. Timur has $q$ queries: each query will be an integer $d$, and Timur wants you to output how many minutes it takes the car to reach point $d$, rounded down to the nearest integer.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains three integers $n$, $k$, and $q$, ($k \\leq n \\leq 10^9$; $1 \\leq k, q \\leq 10^5$) β€” the final destination, the number of points Timur knows the time for, and the number of queries respectively.\n\nThe second line of each test case contains $k$ integers $a_i$ ($1 \\leq a_i \\leq n$; $a_i < a_{i+1}$ for every $1 \\leq i \\leq k-1$; $a_k = n$).\n\nThe third line of each test case contains $k$ integers $b_i$ ($1 \\leq b_i \\leq 10^9$; $b_i < b_{i+1}$ for every $1 \\leq i \\leq k-1$).\n\nEach of the following $q$ lines contains a single integer $d$ ($0 \\leq d \\leq n$) β€” the distance that Timur asks the minutes passed for.\n\nThe sum of $k$ over all test cases doesn't exceed $10^5$, and the sum of $q$ over all test cases doesn't exceed $10^5$.\n\nOutput Format:\nFor each query, output a single integer β€” the number of minutes passed until the car reaches the point $d$, rounded down.\n\nTags:\n['binary search', 'math', 'sortings']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n10 1 3\n10\n10\n0\n6\n7\n10 2 4\n4 10\n4 7\n6\n4\n2\n7\n1000000000 1 1\n1000000000\n1000000000\n99999999\n6 1 3\n6\n5\n2\n6\n5", "metadata": { "func_name": null }, "output": "0 6 7 \n5 4 2 5 \n99999999 \n1 5 4", "testtype": "stdin" } ], "style": "rule" }
{ "index": 4, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nGiven an integer $r$, find the number of lattice points that have a Euclidean distance from $(0, 0)$ greater than or equal to $r$ but strictly less than $r+1$.\n\nA lattice point is a point with integer coordinates. The Euclidean distance from $(0, 0)$ to the point $(x,y)$ is $\\sqrt{x^2 + y^2}$.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) β€” the number of test cases.\n\nThe only line of each test case contains a single integer $r$ ($1 \\leq r \\leq 10^5$).\n\nThe sum of $r$ over all test cases does not exceed $10^5$.\n\nOutput Format:\nFor each test case, output a single integer β€” the number of lattice points that have an Euclidean distance $d$ from $(0, 0)$ such that $r \\leq d < r+1$.\n\nTags:\n['binary search', 'brute force', 'dfs and similar', 'geometry', 'implementation', 'math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n1\n2\n3\n4\n5\n1984", "metadata": { "func_name": null }, "output": "8\n16\n20\n24\n40\n12504", "testtype": "stdin" } ], "style": "rule" }
{ "index": 5, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given an array $a$ consisting of $n$ nonnegative integers.\n\nYou can swap the elements at positions $i$ and $j$ if $a_i~\\mathsf{XOR}~a_j < 4$, where $\\mathsf{XOR}$ is the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).\n\nFind the lexicographically smallest array that can be made with any number of swaps.\n\nAn array $x$ is lexicographically smaller than an array $y$ if in the first position where $x$ and $y$ differ, $x_i < y_i$.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\leq n \\leq 2\\cdot10^5$) β€” the length of the array.\n\nThe second line of each test case contains $n$ integers $a_i$ ($0 \\leq a_i \\leq 10^9$) β€” the elements of the array.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output $n$ integers β€” the lexicographically smallest array that can be made with any number of swaps.\n\nTags:\n['data structures', 'dsu', 'sortings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n4\n1 0 3 2\n5\n2 7 1 5 6\n8\n1 2 1 2 1 2 1 2\n4\n16 4 1 64", "metadata": { "func_name": null }, "output": "0 1 2 3 \n1 5 2 6 7 \n1 1 1 1 2 2 2 2 \n16 4 1 64", "testtype": "stdin" } ], "style": "rule" }
{ "index": 6, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nBob has a grid with $3$ rows and $n$ columns, each of which contains either $a_i$ or $-a_i$ for some integer $1 \\leq i \\leq n$. For example, one possible grid for $n=4$ is shown below:\n\n$$\\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\\\\\ -a_4 & a_4 & -a_1 & -a_3 \\\\\\ a_1 & a_2 & -a_2 & a_4 \\end{bmatrix}$$\n\nAlice and Bob play a game as follows:\n\n * Bob shows Alice his grid. * Alice gives Bob an array $a_1, a_2, \\dots, a_n$ of her choosing, whose elements are all $\\mathbf{-1}$ or $\\mathbf{1}$. * Bob substitutes these values into his grid to make a grid of $-1$s and $1$s. * Bob sorts the elements of each column in non-decreasing order. * Alice wins if all the elements in the middle row are $1$; otherwise, Bob wins. \n\nFor example, suppose Alice gives Bob the array $[1, -1, -1, 1]$ for the grid above. Then the following will happen (colors are added for clarity):\n\n$$\\begin{bmatrix} \\color{red}{a_1} & \\color{green}{-a_2} & \\color{blue}{-a_3} & \\color{green}{-a_2} \\\\\\ -a_4 & a_4 & \\color{red}{-a_1} & \\color{blue}{-a_3} \\\\\\ \\color{red}{a_1} & \\color{green}{a_2} & \\color{green}{-a_2} & a_4 \\end{bmatrix} \\xrightarrow{[\\color{red}{1},\\color{green}{-1},\\color{blue}{-1},1]} \\begin{bmatrix} \\color{red}{1} & \\color{green}{1} & \\color{blue}{1} & \\color{green}{1} \\\\\\ -1 & 1 & \\color{red}{-1} & \\color{blue}{1} \\\\\\ \\color{red}{1} & \\color{green}{-1} & \\color{green}{1} & 1 \\end{bmatrix} \\xrightarrow{\\text{sort each column}} \\begin{bmatrix} -1 & -1 & -1 & 1 \\\\\\ \\mathbf{1} & \\mathbf{1} & \\mathbf{1} & \\mathbf{1} \\\\\\ 1 & 1 & 1 & 1 \\\\\\ \\end{bmatrix}\\,. $$ Since the middle row is all $1$, Alice wins.\n\nGiven Bob's grid, determine whether or not Alice can choose the array $a$ to win the game.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) β€” the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($2 \\leq n \\leq 500$) β€” the number of columns of Bob's grid.\n\nThe next three lines each contain $n$ integers, the $i$-th of which contains $g_{i,1}, g_{i,2}, \\dots, g_{i,n}$ ($-n \\leq g_{i,j} \\leq n$, $g_{i,j} \\neq 0$), representing Bob's grid.\n\nIf cell $x > 0$ is in the input, that cell in Bob's grid should contain $a_x$; if $x < 0$ is in the input, that cell in Bob's grid should contain $-a_{-x}$. See the sample input and notes for a better understanding.\n\nOutput Format:\nFor each test case, output \"YES\" (without quotes) if Alice can win, and \"NO\" (without quotes) otherwise.\n\nYou can output \"YES\" and \"NO\" in any case (for example, strings \"yEs\", \"yes\", and \"Yes\" will be recognized as a positive response).\n\nTags:\n['2-sat', 'dfs and similar', 'graphs']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n4\n1 -2 -3 -2\n-4 4 -1 -3\n1 2 -2 4\n2\n1 2\n-1 -2\n2 -2\n5\n1 2 3 4 5\n-2 3 -4 -5 -1\n3 -5 1 2 2\n6\n1 3 -6 2 5 2\n1 3 -2 -3 -6 -5\n-2 -1 -3 2 3 1", "metadata": { "func_name": null }, "output": "YES\nNO\nYES\nNO", "testtype": "stdin" } ], "style": "rule" }
{ "index": 7, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nA contest contains $n$ problems and the difficulty of the $i$-th problem is expected to be at most $b_i$. There are already $n$ problem proposals and the difficulty of the $i$-th problem is $a_i$. Initially, both $a_1, a_2, \\ldots, a_n$ and $b_1, b_2, \\ldots, b_n$ are sorted in non-decreasing order.\n\nSome of the problems may be more difficult than expected, so the writers must propose more problems. When a new problem with difficulty $w$ is proposed, the most difficult problem will be deleted from the contest, and the problems will be sorted in a way that the difficulties are non-decreasing.\n\nIn other words, in each operation, you choose an integer $w$, insert it into the array $a$, sort array $a$ in non-decreasing order, and remove the last element from it.\n\nFind the minimum number of new problems to make $a_i\\le b_i$ for all $i$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\le t\\le 100$). The description of the test cases follows.\n\nThe first line of each test case contains only one positive integer $n$ ($1 \\leq n \\leq 100$), representing the number of problems.\n\nThe second line of each test case contains an array $a$ of length $n$ ($1\\le a_1\\le a_2\\le\\cdots\\le a_n\\le 10^9$).\n\nThe third line of each test case contains an array $b$ of length $n$ ($1\\le b_1\\le b_2\\le\\cdots\\le b_n\\le 10^9$).\n\nOutput Format:\nFor each test case, print an integer as your answer in a new line.\n\nTags:\n['brute force', 'greedy', 'two pointers']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "2\n6\n1000 1400 2000 2000 2200 2700\n800 1200 1500 1800 2200 3000\n6\n4 5 6 7 8 9\n1 2 3 4 5 6", "metadata": { "func_name": null }, "output": "2\n3", "testtype": "stdin" } ], "style": "rule" }
{ "index": 8, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThere are $n$ coins on the table forming a circle, and each coin is either facing up or facing down. Alice and Bob take turns to play the following game, and Alice goes first.\n\nIn each operation, the player chooses a facing-up coin, removes the coin, and flips the two coins that are adjacent to it. If (before the operation) there are only two coins left, then one will be removed and the other won't be flipped (as it would be flipped twice). If (before the operation) there is only one coin left, no coins will be flipped. If (before the operation) there are no facing-up coins, the player loses.\n\nDecide who will win the game if they both play optimally. It can be proved that the game will end in a finite number of operations, and one of them will win.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\le t\\le 100$). The description of the test cases follows.\n\nThe first line of each test case contains only one positive integer $n$ ($1 \\leq n \\leq 100$), representing the number of the coins.\n\nA string $s$ of length $n$ follows on the second line of each test case, containing only \"U\" and \"D\", representing that each coin is facing up or facing down.\n\nOutput Format:\nFor each test case, print \"YES\" if Alice will win the game, and \"NO\" otherwise.\n\nYou can output the answer in any case (upper or lower). For example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be recognized as positive responses.\n\nTags:\n['games']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n5\nUUDUD\n5\nUDDUD\n2\nUU", "metadata": { "func_name": null }, "output": "YES\nNO\nNO", "testtype": "stdin" } ], "style": "rule" }
{ "index": 9, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.\n\nThere is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy $k$ new cards in total, and the cards you buy can contain any integer between $1$ and $n$.\n\nAfter buying the new cards, you rearrange all your cards in a line. The score of a rearrangement is the number of (contiguous) subarrays of length $n$ which are a permutation of $[1, 2, \\ldots, n]$. What's the maximum score you can get?\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t\\ (1\\le t\\le 100)$. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$, $k$ ($1\\le n \\le 2 \\cdot 10^5$, $0\\le k \\le 10^{12}$) β€” the number of distinct types of cards and the number of coins.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^{12}$) β€” the number of cards of type $i$ you have at the beginning.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $5 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output a single line containing an integer: the maximum score you can get.\n\nTags:\n['binary search', 'constructive algorithms', 'greedy', 'implementation', 'math', 'sortings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "8\n1 10\n1\n2 4\n8 4\n3 4\n6 1 8\n3 9\n7 6 2\n5 3\n6 6 7 4 6\n9 7\n7 6 1 7 6 2 4 3 3\n10 10\n1 3 1 2 1 9 3 5 7 5\n9 8\n5 8 7 5 1 3 2 9 8", "metadata": { "func_name": null }, "output": "11\n15\n15\n22\n28\n32\n28\n36", "testtype": "stdin" } ], "style": "rule" }
{ "index": 10, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThe two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.\n\nYou are given two positive integers $n$, $m$.\n\nCalculate the number of ordered pairs $(a, b)$ satisfying the following conditions:\n\n * $1\\le a\\le n$, $1\\le b\\le m$; * $a+b$ is a multiple of $b \\cdot \\gcd(a,b)$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\le t\\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$, $m$ ($1\\le n,m\\le 2 \\cdot 10^6$).\n\nIt is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \\cdot 10^6$.\n\nOutput Format:\nFor each test case, print a single integer: the number of valid pairs.\n\nTags:\n['brute force', 'math', 'number theory']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n1 1\n2 3\n3 5\n10 8\n100 1233\n1000000 1145141", "metadata": { "func_name": null }, "output": "1\n3\n4\n14\n153\n1643498", "testtype": "stdin" } ], "style": "rule" }
{ "index": 11, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThe two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.\n\nYou are given two positive integers $n$, $m$.\n\nCalculate the number of ordered pairs $(a, b)$ satisfying the following conditions:\n\n * $1\\le a\\le n$, $1\\le b\\le m$; * $b \\cdot \\gcd(a,b)$ is a multiple of $a+b$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\le t\\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$, $m$ ($1\\le n,m\\le 2 \\cdot 10^6$).\n\nIt is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \\cdot 10^6$.\n\nOutput Format:\nFor each test case, print a single integer: the number of valid pairs.\n\nTags:\n['brute force', 'math', 'number theory']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n1 1\n2 3\n3 5\n10 8\n100 1233\n1000000 1145141", "metadata": { "func_name": null }, "output": "0\n1\n1\n6\n423\n5933961", "testtype": "stdin" } ], "style": "rule" }
{ "index": 12, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nLet $\\operatorname{lowbit}(x)$ denote the value of the lowest binary bit of $x$, e.g. $\\operatorname{lowbit}(12)=4$, $\\operatorname{lowbit}(8)=8$.\n\nFor an array $a$ of length $n$, if an array $s$ of length $n$ satisfies $s_k=\\left(\\sum\\limits_{i=k-\\operatorname{lowbit}(k)+1}^{k}a_i\\right)\\bmod 998\\,244\\,353$ for all $k$, then $s$ is called the Fenwick Tree of $a$. Let's denote it as $s=f(a)$.\n\nFor a positive integer $k$ and an array $a$, $f^k(a)$ is defined as follows:\n\n$$ f^k(a)= \\begin{cases} f(a)&\\textrm{if }k=1\\\\\\ f(f^{k-1}(a))&\\textrm{otherwise.}\\\\\\ \\end{cases} $$\n\nYou are given an array $b$ of length $n$ and a positive integer $k$. Find an array $a$ that satisfies $0\\le a_i < 998\\,244\\,353$ and $f^k(a)=b$. It can be proved that an answer always exists. If there are multiple possible answers, you may print any of them.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\le t\\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains two positive integers $n$ ($1 \\leq n \\leq 2\\cdot 10^5$) and $k$ ($1\\le k\\le 10^9$), representing the length of the array and the number of times the function $f$ is performed.\n\nThe second line of each test case contains an array $b_1, b_2, \\ldots, b_n$ ($0\\le b_i < 998\\,244\\,353$).\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2\\cdot 10^5$.\n\nOutput Format:\nFor each test case, print a single line, containing a valid array $a$ of length $n$.\n\nTags:\n['combinatorics', 'data structures', 'math', 'matrices']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "2\n8 1\n1 2 1 4 1 2 1 8\n6 2\n1 4 3 17 5 16", "metadata": { "func_name": null }, "output": "1 1 1 1 1 1 1 1\n1 2 3 4 5 6", "testtype": "stdin" } ], "style": "rule" }
{ "index": 13, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nLittle R is a magician who likes non-decreasing arrays. She has an array of length $n$, initially as $a_1, \\ldots, a_n$, in which each element is an integer between $[1, m]$. She wants it to be non-decreasing, i.e., $a_1 \\leq a_2 \\leq \\ldots \\leq a_n$.\n\nTo do this, she can perform several magic tricks. Little R has a fixed array $b_1\\ldots b_m$ of length $m$. Formally, let's define a trick as a procedure that does the following things in order:\n\n * Choose a set $S \\subseteq \\\\{1, 2, \\ldots, n\\\\}$. * For each $u \\in S$, assign $a_u$ with $b_{a_u}$. \n\nLittle R wonders how many tricks are needed at least to make the initial array non-decreasing. If it is not possible with any amount of tricks, print $-1$ instead.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\le t\\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $m$ ($1\\leq n \\leq 10^6$, $1 \\leq m \\leq 10^6$) β€” the length of the initial array and the range of the elements in the array.\n\nThe second line of each test case contains $n$ integers $a_1, \\ldots, a_n$ ($1 \\leq a_i \\leq m$) β€” the initial array.\n\nThe third line of each test case contains $m$ integers $b_1, \\ldots, b_m$ ($1 \\leq b_i \\leq m$) β€” the fixed magic array.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ and the sum of $m$ over all test cases does not exceed $10^6$.\n\nOutput Format:\nFor each test case, output a single integer: the minimum number of tricks needed, or $-1$ if it is impossible to make $a_1, \\ldots, a_n$ non- decreasing.\n\nTags:\n['binary search', 'dfs and similar', 'graphs', 'implementation']\n\nTime Limit: 4000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n5 8\n1 6 3 7 1\n2 3 5 8 7 1 5 6\n3 3\n1 3 2\n2 1 3\n10 10\n2 8 5 4 8 4 1 5 10 10\n6 7 2 6 3 4 1 1 3 5", "metadata": { "func_name": null }, "output": "3\n-1\n3", "testtype": "stdin" } ], "style": "rule" }
{ "index": 14, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThree friends gathered to play a few games of chess together.\n\nIn every game, two of them play against each other. The winner gets $2$ points while the loser gets $0$, and in case of a draw, both players get $1$ point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all.\n\nYou've been told that their scores after all the games were played were $p_1$, $p_2$ and $p_3$. Additionally, it is guaranteed that $p_1 \\leq p_2 \\leq p_3$ holds.\n\nFind the maximum number of draws that could have happened and print it. If there isn't any way to obtain $p_1$, $p_2$ and $p_3$ as a result of a non-negative number of games between the three players, print $-1$ instead.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 500$). The description of the test cases follows.\n\nThe first line of each test case contains three integers $p_1$, $p_2$ and $p_3$ ($0 \\leq p_1 \\leq p_2 \\leq p_3 \\leq 30$) β€” the scores of the three players, sorted non-decreasingly.\n\nOutput Format:\nFor each testcase, print one number β€” the maximum possible number of draws that could've happened, or $-1$ if the scores aren't consistent with any valid set of games and results.\n\nTags:\n['brute force', 'dp', 'implementation', 'math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n0 0 0\n0 1 1\n1 1 1\n1 1 2\n3 3 3\n3 4 5\n1 1 10", "metadata": { "func_name": null }, "output": "0\n1\n-1\n2\n-1\n6\n2", "testtype": "stdin" } ], "style": "rule" }
{ "index": 15, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nToday, Cat and Fox found an array $a$ consisting of $n$ non-negative integers.\n\nDefine the loneliness of $a$ as the smallest positive integer $k$ ($1 \\le k \\le n$) such that for any two positive integers $i$ and $j$ ($1 \\leq i, j \\leq n - k +1$), the following holds: $$a_i | a_{i+1} | \\ldots | a_{i+k-1} = a_j | a_{j+1} | \\ldots | a_{j+k-1},$$ where $x | y$ denotes the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) of $x$ and $y$. In other words, for every $k$ consecutive elements, their bitwise OR should be the same. Note that the loneliness of $a$ is well-defined, because for $k = n$ the condition is satisfied.\n\nCat and Fox want to know how lonely the array $a$ is. Help them calculate the loneliness of the found array.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\leq t \\leq 10^4 $) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains one integer $n$ ($1 \\leq n \\leq 10^5$) β€” the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_i < 2^{20}$) β€” the elements of the array.\n\nIt is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.\n\nOutput Format:\nFor each test case, print one integer β€” the loneliness of the given array.\n\nTags:\n['binary search', 'bitmasks', 'data structures', 'greedy', 'math', 'two pointers']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n1\n0\n3\n2 2 2\n3\n1 0 2\n5\n3 0 1 4 2\n5\n2 0 4 0 2\n7\n0 0 0 0 1 2 4\n8\n0 1 3 2 2 1 0 3", "metadata": { "func_name": null }, "output": "1\n1\n3\n4\n4\n7\n3", "testtype": "stdin" } ], "style": "rule" }
{ "index": 16, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nFox loves permutations! She came up with the following problem and asked Cat to solve it:\n\nYou are given an even positive integer $n$ and a permutation$^\\dagger$ $p$ of length $n$.\n\nThe score of another permutation $q$ of length $n$ is the number of local maximums in the array $a$ of length $n$, where $a_i = p_i + q_i$ for all $i$ ($1 \\le i \\le n$). In other words, the score of $q$ is the number of $i$ such that $1 < i < n$ (note the strict inequalities), $a_{i-1} < a_i$, and $a_i > a_{i+1}$ (once again, note the strict inequalities).\n\nFind the permutation $q$ that achieves the maximum score for given $n$ and $p$. If there exist multiple such permutations, you can pick any of them.\n\n$^\\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).\n\nInput Format:\nThe first line of input contains an integer $t$ ($1 \\leq t \\leq 10^4$) β€” the number of test cases in the input you will have to solve.\n\nThe first line of each test case contains one even integer $n$ ($4 \\leq n \\leq 10^5$, $n$ is even) β€” the length of the permutation $p$.\n\nThe second line of each test case contains the $n$ integers $p_1, p_2, \\ldots, p_n$ ($1 \\leq p_i \\leq n$). It is guaranteed that $p$ is a permutation of length $n$.\n\nIt is guaranteed that the sum of $n$ across all test cases doesn't exceed $10^5$.\n\nOutput Format:\nFor each test case, output one line containing any permutation of length $n$ (the array $q$), such that $q$ maximizes the score under the given constraints.\n\nTags:\n['constructive algorithms', 'greedy', 'implementation', 'math', 'sortings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n4\n1 2 3 4\n4\n4 3 1 2\n6\n6 5 1 4 2 3\n8\n1 2 4 5 7 6 8 3", "metadata": { "func_name": null }, "output": "2 4 1 3\n3 1 4 2\n2 5 1 4 3 6\n5 4 8 2 7 1 6 3", "testtype": "stdin" } ], "style": "rule" }
{ "index": 17, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThis is an interactive problem.\n\nFox gave Cat two positive integers $n$ and $k$. She has a hidden array $a_1, \\ldots , a_n$ of length $n$, such that $1 \\leq a_i \\leq n$ for every $i$. Now they are going to play the following game:\n\nFor any two integers $l, r$ such that $1 \\leq l \\leq r \\leq n$, define $f(l, r) = (r - l + 1) \\cdot \\max\\limits_{x=l}^r a_x$. In other words, $f(l, r)$ is equal to the maximum of the subarray $a_l, \\ldots, a_r$ multiplied by its size.\n\nCat can ask Fox at most $2 n$ questions about the array. He will tell her two integers $l$ and $x$ ($1 \\leq l \\leq n, 1 \\leq x \\leq 10^9$), and she will tell him one integer $p$ as the answer β€” the smallest positive integer $r$ such that $f(l, r) = x$, or $n+1$ if no such $r$ exists.\n\nNow, Cat needs to find the largest value $m$ such that there exists a sequence $c_1, \\ldots, c_{k-1}$ such that $1 \\leq c_1 < \\ldots < c_{k-1} < n$ and $f(1, c_1) = f(c_1 + 1, c_2) = \\ldots = f(c_{k-1}+1, n) = m$. If no such $m$ exists, he should indicate this and take $-1$ as the answer. Note that for $k = 1$, $m$ is always equal to $f(1, n)$.\n\nIn other words, the goal is to find the largest $m$ such that you can split the array into exactly $k$ subarrays ($k$ is the constant given to you in the beginning of the interaction) so that all the subarrays have the product of their length and their maximum equal to $m$, or determine that no such $m$ exists. Every element should belong in exactly one of the subarrays.\n\nCat doesn't know what he should do, so he asked you to play the game for him.\n\nInput Format:\n\n\nOutput Format:\n\n\nTags:\n['brute force', 'interactive', 'math']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n1 1\n\n1\n2 2\n\n1\n\n3\n\n1\n6 3\n\n7\n\n2\n\n3\n\n6\n\n1", "metadata": { "func_name": null }, "output": "! 1\n\n\n? 1 1\n\n? 2 1\n\n! -1\n\n\n? 1 9\n\n? 1 6\n\n? 3 6\n\n? 4 6\n\n! 6", "testtype": "stdin" } ], "style": "rule" }
{ "index": 18, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nFox has found an array $p_1, p_2, \\ldots, p_n$, that is a permutation of length $n^\\dagger$ of the numbers $1, 2, \\ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her β€” he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \\leq x + y \\leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.\n\nThey don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \\leq l \\leq r \\leq 2 \\cdot n$.\n\nYou are given the number $n$ and the array $p_1, p_2, \\ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \\leq x + y \\leq r$ (arbitrary number of times, possibly $0$).\n\n$^\\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The description of the test cases follows.\n\nDescription of each test case consists of two lines. The first line contains one integer $n$ ($1 \\leq n \\leq 10^5$).\n\nThe second line contains $n$ integers: the array $p_1, p_2, \\ldots, p_n$ ($1 \\le p_i \\le n$). It is guaranteed that this array is a permutation of length $n$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput Format:\nFor each test case, print the number of pairs of integers $(l, r)$ such that $1 \\leq l \\leq r \\leq 2 \\cdot n$, and you can sort the array under the constraints.\n\nTags:\n['graphs', 'math', 'sortings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n2\n2 1\n3\n3 1 2\n4\n3 2 1 4\n5\n5 3 1 2 4\n5\n1 2 3 4 5\n6\n3 2 1 5 4 6\n6\n1 3 2 4 5 6", "metadata": { "func_name": null }, "output": "6\n11\n23\n29\n55\n46\n58", "testtype": "stdin" } ], "style": "rule" }
{ "index": 19, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nFor $k$ positive integers $x_1, x_2, \\ldots, x_k$, the value $\\gcd(x_1, x_2, \\ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \\ldots, x_k$ β€” the largest integer $z$ such that all the integers $x_1, x_2, \\ldots, x_k$ are divisible by $z$.\n\nYou are given three arrays $a_1, a_2, \\ldots, a_n$, $b_1, b_2, \\ldots, b_n$ and $c_1, c_2, \\ldots, c_n$ of length $n$, containing positive integers.\n\nYou also have a machine that allows you to swap $a_i$ and $b_i$ for any $i$ ($1 \\le i \\le n$). Each swap costs you $c_i$ coins.\n\nFind the maximum possible value of $$\\gcd(a_1, a_2, \\ldots, a_n) + \\gcd(b_1, b_2, \\ldots, b_n)$$ that you can get by paying in total at most $d$ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the $q$ possible values $d_1, d_2, \\ldots, d_q$.\n\nInput Format:\nThere are two integers on the first line β€” the numbers $n$ and $q$ ($1 \\leq n \\leq 5 \\cdot 10^5$, $1 \\leq q \\leq 5 \\cdot 10^5$).\n\nOn the second line, there are $n$ integers β€” the numbers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^8$).\n\nOn the third line, there are $n$ integers β€” the numbers $b_1, b_2, \\ldots, b_n$ ($1 \\leq b_i \\leq 10^8$).\n\nOn the fourth line, there are $n$ integers β€” the numbers $c_1, c_2, \\ldots, c_n$ ($1 \\leq c_i \\leq 10^9$).\n\nOn the fifth line, there are $q$ integers β€” the numbers $d_1, d_2, \\ldots, d_q$ ($0 \\leq d_i \\leq 10^{15}$).\n\nOutput Format:\nPrint $q$ integers β€” the maximum value you can get for each of the $q$ possible values $d$.\n\nTags:\n['bitmasks', 'brute force', 'dp', 'implementation', 'number theory']\n\nTime Limit: 5000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3 4\n1 2 3\n4 5 6\n1 1 1\n0 1 2 3", "metadata": { "func_name": null }, "output": "2 3 3 3", "testtype": "stdin" }, { "input": "5 5\n3 4 6 8 4\n8 3 4 9 3\n10 20 30 40 50\n5 55 13 1000 113", "metadata": { "func_name": null }, "output": "2 7 3 7 7", "testtype": "stdin" }, { "input": "1 1\n3\n4\n5\n0", "metadata": { "func_name": null }, "output": "7", "testtype": "stdin" } ], "style": "rule" }
{ "index": 20, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nLittle Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size $5 \\times 3$, i.e., five rows and three columns.\n\nThere are $x$ applications with an icon size of $1 \\times 1$ cells; such an icon occupies only one cell of the screen. There are also $y$ applications with an icon size of $2 \\times 2$ cells; such an icon occupies a square of $4$ cells on the screen. Each cell of each screen can be occupied by no more than one icon.\n\nRosie wants to place the application icons on the minimum number of screens. Help her find the minimum number of screens needed.\n\nInput Format:\nThe first line of the input contains $t$ ($1 \\leq t \\leq 10^4$) β€” the number of test cases.\n\nThe first and only line of each test case contains two integers $x$ and $y$ ($0 \\leq x, y \\leq 99$) β€” the number of applications with a $1 \\times 1$ icon and the number of applications with a $2 \\times 2$ icon, respectively.\n\nOutput Format:\nFor each test case, output the minimal number of required screens on a separate line.\n\nTags:\n['greedy', 'math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "11\n1 1\n7 2\n12 4\n0 3\n1 0\n8 1\n0 0\n2 0\n15 0\n8 2\n0 9", "metadata": { "func_name": null }, "output": "1\n1\n2\n2\n1\n1\n0\n1\n1\n2\n5", "testtype": "stdin" } ], "style": "rule" }
{ "index": 21, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nPolycarp has a string $s$, which consists of lowercase Latin letters. He encodes this string using the following algorithm:\n\n * first, he constructs a new auxiliary string $r$, which consists of all distinct letters of the string $s$, written in alphabetical order; * then the encoding happens as follows: each character in the string $s$ is replaced by its symmetric character from the string $r$ (the first character of the string $r$ will be replaced by the last, the second by the second from the end, and so on). \n\nFor example, encoding the string $s$=\"codeforces\" happens as follows:\n\n * the string $r$ is obtained as \"cdefors\"; * the first character $s_1$='c' is replaced by 's'; * the second character $s_2$='o' is replaced by 'e'; * the third character $s_3$='d' is replaced by 'r'; * ... * the last character $s_{10}$='s' is replaced by 'c'. \n\n![](CDN_BASE_URL/e120531cc984becdbbab53f80507a062) The string $r$ and replacements for $s$=\"codeforces\".\n\nThus, the result of encoding the string $s$=\"codeforces\" is the string \"serofedsoc\".\n\nWrite a program that performs decoding β€” that is, restores the original string $s$ from the encoding result.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) β€” the length of the string $b$.\n\nThe second line of each test case contains a string $b$ of length $n$, consisting of lowercase Latin letters β€” the result of encoding the original string $s$.\n\nIt is guaranteed that the sum of the values of $n$ over all test cases in the test does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output the string $s$ from which the encoding result $b$ was obtained.\n\nTags:\n['implementation', 'sortings', 'strings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n10\nserofedsoc\n3\nttf\n9\ntlrhgmaoi\n1\nw\n15\nhnndledmnhlttin", "metadata": { "func_name": null }, "output": "codeforces\nfft\nalgorithm\nw\nmeetinthemiddle", "testtype": "stdin" } ], "style": "rule" }
{ "index": 22, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nPolycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \\le j \\le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$.\n\nPolycarp considers a pair of triples $b$ and $c$ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:\n\n * $b_1 \\ne c_1$ and $b_2 = c_2$ and $b_3 = c_3$; * $b_1 = c_1$ and $b_2 \\ne c_2$ and $b_3 = c_3$; * $b_1 = c_1$ and $b_2 = c_2$ and $b_3 \\ne c_3$. \n\nFind the number of beautiful pairs of triples among the written triples $[a_j, a_{j + 1}, a_{j + 2}]$.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($3 \\le n \\le 2 \\cdot 10^5$) β€” the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^6$) β€” the elements of the array.\n\nIt is guaranteed that the sum of the values of $n$ for all test cases in the test does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output a single integer β€” the number of beautiful pairs of triples among the pairs of the form $[a_j, a_{j + 1}, a_{j + 2}]$.\n\nNote that the answer may not fit into 32-bit data types.\n\nTags:\n['combinatorics', 'data structures']\n\nTime Limit: 4000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "8\n5\n3 2 2 2 3\n5\n1 2 1 2 1\n8\n1 2 3 2 2 3 4 2\n4\n2 1 1 1\n8\n2 1 1 2 1 1 1 1\n7\n2 1 1 1 1 1 1\n6\n2 1 1 1 1 1\n5\n2 1 1 1 1", "metadata": { "func_name": null }, "output": "2\n0\n3\n1\n8\n4\n3\n2", "testtype": "stdin" } ], "style": "rule" }
{ "index": 23, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nLet's imagine the surface of Mars as an infinite coordinate plane. Initially, the rover Perseverance-2 and the helicopter Ingenuity-2 are located at the point with coordinates $(0, 0)$. A set of instructions $s$ consisting of $n$ instructions of the following types was specially developed for them:\n\n * N: move one meter north (from point $(x, y)$ to $(x, y + 1)$); * S: move one meter south (from point $(x, y)$ to $(x, y - 1)$); * E: move one meter east (from point $(x, y)$ to $(x + 1, y)$); * W: move one meter west (from point $(x, y)$ to $(x - 1, y)$). \n\nEach instruction must be executed either by the rover or by the helicopter. Moreover, each device must execute at least one instruction. Your task is to distribute the instructions in such a way that after executing all $n$ instructions, the helicopter and the rover end up at the same point, or determine that this is impossible.\n\nInput Format:\nThe first line of input contains $t$ ($1 \\leq t \\leq 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) β€” the number of instructions.\n\nThe second line of each test case contains a string $s$ of length $n$ consisting of the characters 'N', 'S', 'E', 'W' β€” the sequence of instructions.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10 ^ 5$.\n\nOutput Format:\nFor each test case, if the required distribution of instructions exists, output a string $p$ of length $n$ consisting of the characters 'R', 'H'. If the $i$-th operation should be executed by the rover, then $p_i=\\text{R}$, if the $i$-th operation should be executed by the helicopter, then $p_i=\\text{H}$. If there are multiple solutions, output any of them.\n\nOtherwise, output NO.\n\nTags:\n['constructive algorithms', 'greedy', 'implementation']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "10\n6\nNENSNE\n3\nWWW\n6\nNESSWS\n2\nSN\n2\nWE\n4\nSSNN\n4\nWESN\n2\nSS\n4\nEWNN\n4\nWEWE", "metadata": { "func_name": null }, "output": "RRHRRH\nNO\nHRRHRH\nNO\nNO\nRHRH\nRRHH\nRH\nRRRH\nRRHH", "testtype": "stdin" } ], "style": "rule" }
{ "index": 24, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nBeing a physicist, Charlie likes to plan his life in simple and precise terms.\n\nFor the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \\le i \\le m)$, there'll be a single opportunity of paying cost $c_i$ pounds to obtain happiness $h_i$.\n\nBorrowing is not allowed. Money earned in the $i$-th month can only be spent in a later $j$-th month ($j>i$).\n\nSince physicists don't code, help Charlie find the maximum obtainable sum of happiness.\n\nInput Format:\nThe first line of input contains a single integer $t$ ($1 \\le t \\le 1000$) β€” the number of test cases.\n\nThe first line of each test case contains two integers, $m$ and $x$ ($1 \\le m \\le 50$, $1 \\le x \\le 10^8$) β€” the total number of months and the monthly salary.\n\nThe $i$-th of the following $m$ lines contains two integers, $c_i$ and $h_i$ ($0 \\le c_i \\le 10^8$, $1 \\le h_i \\le 10^3$) β€” the cost and happiness on offer for the $i$-th month. Note that some happiness may be free ($c_i=0$ for some $i$'s).\n\nIt is guaranteed that the sum of $\\sum_i h_i$ over all test cases does not exceed $10^5$.\n\nOutput Format:\nFor each test case, print a single integer, the maximum sum of happiness Charlie could obtain.\n\nTags:\n['dp']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n1 10\n1 5\n2 80\n0 10\n200 100\n3 100\n70 100\n100 200\n150 150\n5 8\n3 1\n5 3\n3 4\n1 5\n5 3\n2 5\n1 5\n2 1\n5 3\n2 5\n2 4\n4 1\n5 1\n3 4\n5 2\n2 1\n1 2\n3 5\n3 2\n3 2", "metadata": { "func_name": null }, "output": "0\n10\n200\n15\n1\n9\n9", "testtype": "stdin" } ], "style": "rule" }
{ "index": 25, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nAlice and Bob were playing a game again. They have a grid of size $a \\times b$ ($1 \\le a, b \\le 10^9$), on which there are $n$ chips, with at most one chip in each cell. The cell at the intersection of the $x$-th row and the $y$-th column has coordinates $(x, y)$.\n\nAlice made the first move, and the players took turns. On each move, a player could cut several (but not all) rows or columns from the beginning or end of the remaining grid and earn a point for each chip that was on the cut part of the grid. Each move can be described by the character 'U', 'D', 'L', or 'R' and an integer $k$:\n\n * If the character is 'U', then the first $k$ remaining rows will be cut; * If the character is 'D', then the last $k$ remaining rows will be cut; * If the character is 'L', then the first $k$ remaining columns will be cut; * If the character is 'R', then the last $k$ remaining columns will be cut. \n\nBased on the initial state of the grid and the players' moves, determine the number of points earned by Alice and Bob, respectively.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains four integers $a$, $b$, $n$, and $m$ ($2 \\le a, b \\le 10^9$, $1 \\le n, m \\le 2 \\cdot 10^5$) β€” the dimensions of the grid, the number of chips, and the number of moves.\n\nEach of the next $n$ lines contain two integers $x_i$ and $y_i$ ($1 \\le x_i \\le a$, $1 \\le y_i \\le b$) β€” the coordinates of the chips. All pairs of coordinates are distinct.\n\nEach of the next $m$ lines contain a character $c_j$ and an integer $k_j$ β€” the description of the $j$-th move. It is guaranteed that $k$ is less than the number of rows/columns in the current grid. In other words, a player cannot cut the entire remaining grid on their move.\n\nIt is guaranteed that the sum of the values of $n$ across all test cases in the test does not exceed $2 \\cdot 10^5$. It is guaranteed that the sum of the values of $m$ across all test cases in the test does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output two integers β€” the number of points earned by Alice and Bob, respectively.\n\nTags:\n['binary search', 'brute force', 'data structures', 'implementation', 'sortings', 'two pointers']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n4 4 3 2\n4 1\n3 3\n2 4\nD 2\nR 1\n4 4 3 3\n4 1\n3 2\n2 3\nD 1\nL 1\nU 2\n3 5 3 2\n1 3\n2 2\n3 3\nR 2\nR 2\n6 4 4 2\n1 4\n2 3\n5 3\n1 1\nR 1\nU 1\n9 3 2 1\n6 1\n3 3\nD 8\n10 10 2 5\n7 5\n9 1\nR 1\nL 2\nD 1\nU 4\nD 1", "metadata": { "func_name": null }, "output": "2 1\n2 0\n0 3\n1 1\n2 0\n0 1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 26, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou can never buy enough happiness, so here we go again! In this version, you can only buy $h_i = 1$ unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation.\n\nBeing a physicist, Charlie likes to plan his life in simple and precise terms.\n\nFor the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \\le i \\le m)$, there'll be a single opportunity of paying cost $c_i$ pounds to obtain one unit of happiness. You cannot buy more than one unit each month.\n\nBorrowing is not allowed. Money earned in the $i$-th month can only be spent in a later $j$-th month ($j>i$).\n\nSince physicists don't code, help Charlie find the maximum reachable units of happiness.\n\nInput Format:\nThe first line of the input contains $t$ ($1 \\leq t \\leq 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains two integers, $m$ and $x$ ($1 \\le m \\le 2 \\cdot 10^5$, $1 \\le x \\le 10^3$) β€” the total number of months and the monthly salary.\n\nThe second line of each test case contains $m$ integers $c_1, c_2, \\dots, c_m$ ($1 \\leq c_i \\leq 10^3$) β€” the cost of one unit of happiness for each month.\n\nIt is guaranteed that sum of $m$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output one integer β€” the maximal amount of happiness Charlie can get.\n\nTags:\n['data structures', 'greedy', 'sortings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n3 3\n2 2 2\n6 5\n2 2 8 2 6 8\n6 4\n4 10 3 8 6 10\n2 1\n1 1\n4 1\n4 1 3 1\n4 2\n1 3 4 3", "metadata": { "func_name": null }, "output": "2\n4\n3\n1\n2\n1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 27, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nMocha likes arrays, so before her departure, Bazoka gave her an array $a$ consisting of $n$ positive integers as a gift.\n\nNow Mocha wants to know whether array $a$ could become sorted in non- decreasing order after performing the following operation some (possibly, zero) times:\n\n * Split the array into two parts β€” a prefix and a suffix, then swap these two parts. In other words, let $a=x+y$. Then, we can set $a:= y+x$. Here $+$ denotes the array concatenation operation. \n\nFor example, if $a=[3,1,4,1,5]$, we can choose $x=[3,1]$ and $y=[4,1,5]$, satisfying $a=x+y$. Then, we can set $a:= y + x = [4,1,5,3,1]$. We can also choose $x=[3,1,4,1,5]$ and $y=[\\,]$, satisfying $a=x+y$. Then, we can set $a := y+x = [3,1,4,1,5]$. Note that we are not allowed to choose $x=[3,1,1]$ and $y=[4,5]$, neither are we allowed to choose $x=[1,3]$ and $y=[5,1,4]$, as both these choices do not satisfy $a=x+y$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\leq t\\leq 1000$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2\\leq n\\leq 50$) β€” the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($1\\leq a_i \\leq 10^6$) β€” the elements of array $a$.\n\nOutput Format:\nFor each test case, output \"Yes\" if $a$ could become non-decreasing after performing the operation any number of times, and output \"No\" if not.\n\nYou can output \"Yes\" and \"No\" in any case (for example, strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive response).\n\nTags:\n['brute force', 'greedy', 'implementation', 'sortings']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n6\n1 1 4 5 1 4\n5\n7 9 2 2 3\n3\n1 2 3", "metadata": { "func_name": null }, "output": "No\nYes\nYes", "testtype": "stdin" } ], "style": "rule" }
{ "index": 28, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nMocha likes arrays, so before her departure, 378QAQ gave her an array $a$ consisting of $n$ positive integers as a gift.\n\nMocha thinks that $a$ is beautiful if there exist two numbers $i$ and $j$ ($1\\leq i,j\\leq n$, $i\\neq j$) such that for all $k$ ($1 \\leq k \\leq n$), $a_k$ is divisible$^\\dagger$ by either $a_i$ or $a_j$.\n\nDetermine whether $a$ is beautiful.\n\n$^\\dagger$ $x$ is divisible by $y$ if there exists an integer $z$ such that $x = y \\cdot z$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\leq t\\leq 500$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($3\\leq n\\leq 10^5$) β€” the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($1\\leq a_i \\leq 10^9$) β€” the elements of the array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput Format:\nFor each test case, output \"Yes\" if array $a$ is beautiful, and output \"No\" otherwise.\n\nYou can output \"Yes\" and \"No\" in any case (for example, strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive response).\n\nTags:\n['brute force', 'greedy', 'math', 'sortings']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n3\n7 3 8\n5\n7 1 9 3 5\n5\n4 12 2 6 3\n5\n7 49 9 3 1000000000", "metadata": { "func_name": null }, "output": "No\nYes\nYes\nNo", "testtype": "stdin" } ], "style": "rule" }
{ "index": 29, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nMocha likes arrays, so before her departure, Chamo gave her an array $a$ consisting of $n$ positive integers as a gift.\n\nMocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times:\n\n 1. Choose indices $l$ and $r$ ($1 \\leq l < r \\leq n$) 2. Let $x$ be the median$^\\dagger$ of the subarray $[a_l, a_{l+1},\\ldots, a_r]$ 3. Set all values $a_l, a_{l+1},\\ldots, a_r$ to $x$ \n\nSuppose $a=[1,2,3,4,5]$ initially:\n\n * If Mocha chooses $(l,r)=(3,4)$ in the first operation, then $x=3$, the array will be changed into $a=[1,2,3,3,5]$. * If Mocha chooses $(l,r)=(1,3)$ in the first operation, then $x=2$, the array will be changed into $a=[2,2,2,4,5]$. \n\nMocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number.\n\n$^\\dagger$ The median in an array $b$ of length $m$ is an element that occupies position number $\\lfloor \\frac{m+1}{2} \\rfloor$ after we sort the elements in non-decreasing order. For example, the median of $[3,1,4,1,5]$ is $3$ and the median of $[5,25,20,24]$ is $20$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\leq t\\leq 500$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2\\leq n\\leq 10^5$) β€” the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($1\\leq a_i \\leq 10^9$) β€” the elements of the array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput Format:\nFor each test case, output the maximum value of the number.\n\nTags:\n['binary search', 'brute force', 'greedy']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "2\n2\n1 2\n5\n1 2 3 4 5", "metadata": { "func_name": null }, "output": "1\n4", "testtype": "stdin" } ], "style": "rule" }
{ "index": 30, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\n378QAQ has a tree with $n$ vertices. Initially, all vertices are white.\n\nThere are two chess pieces called $P_A$ and $P_B$ on the tree. $P_A$ and $P_B$ are initially located on vertices $a$ and $b$ respectively. In one step, 378QAQ will do the following in order:\n\n 1. Move $P_A$ to a neighboring vertex. If the target vertex is white, this vertex will be painted red. 2. Move $P_B$ to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue. \n\nInitially, the vertex $a$ is painted red. If $a=b$, the vertex $a$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.\n\n378QAQ wants to know the minimum number of steps to paint all vertices blue.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\leq t\\leq 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains one integer $n$ ($1\\leq n\\leq 2\\cdot 10^5$).\n\nThe second line of each test case contains two integers $a$ and $b$ ($1\\leq a,b\\leq n$).\n\nThen $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \\le x_i,y_i \\le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2\\cdot 10^5$.\n\nOutput Format:\nFor each test case, output the minimum number of steps to paint all vertices blue.\n\nTags:\n['brute force', 'dfs and similar', 'dp', 'greedy', 'shortest paths', 'trees']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n2\n1 2\n1 2\n5\n1 2\n1 2\n1 3\n1 4\n1 5\n8\n5 4\n7 1\n1 5\n1 8\n8 3\n7 2\n8 6\n3 4", "metadata": { "func_name": null }, "output": "2\n8\n13", "testtype": "stdin" } ], "style": "rule" }
{ "index": 31, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given a tree of $n$ vertices numbered from $1$ to $n$. Initially, all vertices are colored white or black.\n\nYou are asked to perform $q$ queries:\n\n * \"u\" β€” toggle the color of vertex $u$ (if it was white, change it to black and vice versa). \n\nAfter each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\leq t\\leq 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $q$ ($1\\leq n,q\\leq 2\\cdot 10^5$).\n\nThe second line of each test case contains $n$ integers $c_1,c_2,\\ldots,c_n$ ($c_i \\in \\\\{ \\mathtt{0}, \\mathtt{1} \\\\}$) β€” the initial color of the vertices. $c_i$ denotes the color of vertex $i$ where $\\mathtt{0}$ denotes the color white, and $\\mathtt{1}$ denotes the color black.\n\nThen $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \\le x_i,y_i \\le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.\n\nThe following $q$ lines each contain an integer $u_i$ ($1 \\le u_i \\le n$), indicating the color of vertex $u_i$ needs to be toggled.\n\nIt is guaranteed that the sum of $n$ and $q$ over all test cases respectively does not exceed $2\\cdot 10^5$.\n\nOutput Format:\nFor each query, output \"Yes\" if the black vertices form a chain, and output \"No\" otherwise.\n\nYou can output \"Yes\" and \"No\" in any case (for example, strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive response).\n\nTags:\n['binary search', 'data structures', 'dfs and similar', 'implementation', 'trees']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "2\n2 1\n1 0\n1 2\n1\n5 4\n1 0 0 0 0\n1 2\n1 3\n1 5\n3 4\n4\n3\n2\n5", "metadata": { "func_name": null }, "output": "No\nNo\nYes\nYes\nNo", "testtype": "stdin" }, { "input": "4\n5 3\n1 1 1 1 1\n3 5\n2 5\n3 4\n1 5\n1\n1\n1\n4 4\n0 0 0 0\n1 2\n2 3\n1 4\n1\n2\n3\n2\n1 1\n1\n1\n1 1\n0\n1", "metadata": { "func_name": null }, "output": "Yes\nNo\nYes\nYes\nYes\nYes\nNo\nNo\nYes", "testtype": "stdin" } ], "style": "rule" }
{ "index": 32, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nDefine the binary encoding of a finite set of natural numbers $T \\subseteq \\\\{0,1,2,\\ldots\\\\}$ as $f(T) = \\sum\\limits_{i \\in T} 2^i$. For example, $f(\\\\{0,2\\\\}) = 2^0 + 2^2 = 5$ and $f(\\\\{\\\\}) = 0$. Notice that $f$ is a bijection from all such sets to all non-negative integers. As such, $f^{-1}$ is also defined.\n\nYou are given an integer $n$ along with $2^n-1$ sets $V_1,V_2,\\ldots,V_{2^n-1}$.\n\nFind all sets $S$ that satisfy the following constraint:\n\n * $S \\subseteq \\\\{0,1,\\ldots,n-1\\\\}$. Note that $S$ can be empty. * For all non-empty subsets $T \\subseteq \\\\{0,1,\\ldots,n-1\\\\}$, $|S \\cap T| \\in V_{f(T)}$. \n\nDue to the large input and output, both input and output will be given in terms of binary encodings of the sets.\n\nInput Format:\nThe first line of input contains a single integer $n$ ($1 \\leq n \\leq 20$).\n\nThe second line of input contains $2^n-1$ integers $v_1,v_2,\\ldots,v_{2^n-1}$ ($0 \\leq v_i < 2^{n+1}$) β€” the sets $V_i$ given in their binary encoding where $V_i = f^{-1}(v_i)$.\n\nOutput Format:\nThe first line of output should contain an integer $k$ indicating the number of possible $S$.\n\nIn the following $k$ lines, you should output $f(S)$ for all possible $S$ in increasing order.\n\nTags:\n['bitmasks', 'brute force', 'combinatorics', 'dfs and similar', 'divide and conquer', 'dp', 'math']\n\nTime Limit: 2000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n15 15 15 15 15 15 12", "metadata": { "func_name": null }, "output": "4\n3\n5\n6\n7", "testtype": "stdin" }, { "input": "5\n63 63 63 63 6 63 63 63 63 63 63 5 63 63 63 63 63 63 8 63 63 63 63 2 63 63 63 63 63 63 63", "metadata": { "func_name": null }, "output": "1\n19", "testtype": "stdin" } ], "style": "rule" }
{ "index": 33, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nOne day, Zimpha casually came up with a problem. As a member of \"Zimpha fan club\", you decided to solve that problem. You are given two strings $s$ and $t$ of length $n$ and $m$, respectively. Both strings only consist of lowercase English letters, - and *.\n\nYou need to replace all occurrences of * and -, observing the following rules:\n\n * For each -, you must replace it with any lowercase English letter. * For each *, you must replace it with a string of any (possibly, zero) length which only consists of lowercase English letters. \n\nNote that you can replace two different instances of - with different characters. You can also replace each two different instances of * with different strings.\n\nSuppose $s$ and $t$ have been transformed into $s'$ and $t'$. Now you're wondering if there's a replacement that makes $s'=t'$.\n\nInput Format:\nThe first line of input contains two integers $n$ and $m$ ($1 \\leq n, m \\leq 2 \\cdot 10^6$) β€” the length of the strings $s$ and $t$, respectively.\n\nThe second line contains the string $s$ of length $n$. It is guaranteed that $s$ only consists of lowercase English letters, - and *.\n\nThe third line contains the string $t$ of length $m$. It is guaranteed that $t$ only consists of lowercase English letters, - and *.\n\nOutput Format:\nFor each test case, output \"Yes\" if there is a replacement that makes $s'=t'$, and output \"No\" otherwise.\n\nYou can output \"Yes\" and \"No\" in any case (for example, strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive response).\n\nTags:\n['fft', 'greedy', 'math', 'strings']\n\nTime Limit: 12000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "10 10\njustmonika\nj-stsayori", "metadata": { "func_name": null }, "output": "No", "testtype": "stdin" }, { "input": "7 8\nttk-wxx\n*tt-l-xx", "metadata": { "func_name": null }, "output": "Yes", "testtype": "stdin" }, { "input": "13 11\nasoulwangziji\n-soulg*z-y-", "metadata": { "func_name": null }, "output": "No", "testtype": "stdin" }, { "input": "7 3\nabc*cba\na*c", "metadata": { "func_name": null }, "output": "No", "testtype": "stdin" }, { "input": "20 18\nbulijiojio-dibuliduo\n*li*ji-*ox*i*-du*-", "metadata": { "func_name": null }, "output": "Yes", "testtype": "stdin" } ], "style": "rule" }
{ "index": 34, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\n378QAQ has a string $s$ of length $n$. Define the core of a string as the substring$^\\dagger$ with maximum lexicographic$^\\ddagger$ order.\n\nFor example, the core of \"$\\mathtt{bazoka}$\" is \"$\\mathtt{zoka}$\", and the core of \"$\\mathtt{aaa}$\" is \"$\\mathtt{aaa}$\".\n\n378QAQ wants to rearrange the string $s$ so that the core is lexicographically minimum. Find the lexicographically minimum possible core over all rearrangements of $s$.\n\n$^\\dagger$ A substring of string $s$ is a continuous segment of letters from $s$. For example, \"$\\mathtt{defor}$\", \"$\\mathtt{code}$\" and \"$\\mathtt{o}$\" are all substrings of \"$\\mathtt{codeforces}$\" while \"$\\mathtt{codes}$\" and \"$\\mathtt{aaa}$\" are not.\n\n$^\\ddagger$ A string $p$ is lexicographically smaller than a string $q$ if and only if one of the following holds:\n\n * $p$ is a prefix of $q$, but $p \\ne q$; or * in the first position where $p$ and $q$ differ, the string $p$ has a smaller element than the corresponding element in $q$ (when compared by their ASCII code). \n\nFor example, \"$\\mathtt{code}$\" and \"$\\mathtt{coda}$\" are both lexicographically smaller than \"$\\mathtt{codeforces}$\" while \"$\\mathtt{codeforceston}$\" and \"$\\mathtt{z}$\" are not.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\leq t\\leq 10^5$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1\\leq n\\leq 10^6$) β€” the length of string $s$.\n\nThe next line of each test case contains the string $s$ of length $n$. The string $s$ consists of lowercase English letters.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.\n\nOutput Format:\nFor each test case, output the lexicographically minimum possible core over all rearrangements of $s$.\n\nTags:\n['greedy', 'strings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n3\nqaq\n4\ncccc\n6\nbazoka\n6\nzazzzz\n7\nababbbb\n7\nccbabcc", "metadata": { "func_name": null }, "output": "qaq\ncccc\nz\nzzz\nbbababb\ncbcacbc", "testtype": "stdin" } ], "style": "rule" }
{ "index": 35, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThis is the way it always was.\n\nThis is the way it always will be.\n\nAll will be forgotten again soon...\n\nJellyfish is playing a one-player card game called \"Slay the Spire\". There are $n$ cards in total numbered from $1$ to $n$. The $i$-th card has power $c_i$.\n\nThere is a binary string $s$ of length $n$. If $s_i = \\texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \\texttt{1}$, the $i$-th card is initially in Jellyfish's hand.\n\nJellyfish will repeat the following process until either her hand or the draw pile is empty.\n\n 1. Let $x$ be the power of the card with the largest power in her hand. 2. Place a single card with power $x$ back into the draw pile. 3. Randomly draw $x$ cards from the draw pile. All subsets of $x$ cards from the draw pile have an equal chance of being drawn. If there are fewer than $x$ cards in the draw pile, Jellyfish will draw all cards. \n\nAt the end of this process, find the probability that Jellyfish can empty the draw pile, modulo $1\\,000\\,000\\,007$.\n\nFormally, let $M=1\\,000\\,000\\,007$. It can be shown that the answer can be expressed as an irreducible fraction $\\frac{p}{q}$, where $p$ and $q$ are integers and $q \\not \\equiv 0 \\pmod{M}$. Output the integer equal to $p \\cdot q^{-1} \\bmod M$. In other words, output such an integer $x$ that $0 \\le x < M$ and $x \\cdot q \\equiv p \\pmod{M}$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1\\leq t\\leq 100$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\leq n \\leq 120$) β€” the number of cards.\n\nThe second line of each test case contains $n$ integers $c_1,c_2,\\ldots,c_n$ ($0 \\leq c_i \\leq n$) β€” the powers of the cards. It is guaranteed that $c_1 \\leq c_2 \\leq \\ldots \\leq c_n$.\n\nThe third line of each test case contains a binary string $s$ of length $n$. If $s_i = \\texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \\texttt{1}$, the $i$-th card is initially in Jellyfish's hand.\n\nIt is guaranteed that the sum of $n^2$ over all test cases does not exceed $120^2$.\n\nOutput Format:\nFor each test case, output the probability that Jellyfish can empty the draw pile modulo $1\\,000\\,000\\,007$.\n\nTags:\n['dp']\n\nTime Limit: 5000 ms\nMemory Limit: 1024 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n5\n0 1 1 1 2\n00100\n3\n2 3 3\n000\n10\n0 0 0 0 0 0 0 1 1 1\n1111011111\n20\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 3 3 4\n00000000001000101010", "metadata": { "func_name": null }, "output": "500000004\n0\n0\n675898154", "testtype": "stdin" } ], "style": "rule" }
{ "index": 36, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nMonocarp is working on his new site, and the current challenge is to make the users pick strong passwords.\n\nMonocarp decided that strong passwords should satisfy the following conditions:\n\n * password should consist only of lowercase Latin letters and digits; * there should be no digit that comes after a letter (so, after each letter, there is either another letter or the string ends); * all digits should be sorted in the non-decreasing order; * all letters should be sorted in the non-decreasing order. \n\nNote that it's allowed for the password to have only letters or only digits.\n\nMonocarp managed to implement the first condition, but he struggles with the remaining ones. Can you help him to verify the passwords?\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 1000$) β€” the number of testcases.\n\nThe first line of each testcase contains a single integer $n$ ($1 \\le n \\le 20$) β€” the length of the password.\n\nThe second line contains a string, consisting of exactly $n$ characters. Each character is either a lowercase Latin letter or a digit.\n\nOutput Format:\nFor each testcase, print \"YES\" if the given password is strong and \"NO\" otherwise.\n\nTags:\n['implementation', 'sortings', 'strings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n4\n12ac\n5\n123wa\n9\nallllmost\n5\nac123\n6\n011679", "metadata": { "func_name": null }, "output": "YES\nNO\nYES\nNO\nYES", "testtype": "stdin" } ], "style": "rule" }
{ "index": 37, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given two integer arrays: array $a$ of length $n$ and array $b$ of length $n+1$.\n\nYou can perform the following operations any number of times in any order:\n\n * choose any element of the array $a$ and increase it by $1$; * choose any element of the array $a$ and decrease it by $1$; * choose any element of the array $a$, copy it and append the copy to the end of the array $a$. \n\nYour task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array $a$ into the array $b$. It can be shown that under the constraints of the problem, it is always possible.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nEach test case consists of three lines:\n\n * the first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$); * the second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$); * the third line contains $n + 1$ integers $b_1, b_2, \\dots, b_{n + 1}$ ($1 \\le b_i \\le 10^9$). \n\nAdditional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, print a single integer β€” the minimum number of operations (possibly zero) required to transform the array $a$ into the array $b$.\n\nTags:\n['greedy', 'implementation']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n1\n2\n1 3\n2\n3 3\n3 3 3\n4\n4 2 1 2\n2 1 5 2 3", "metadata": { "func_name": null }, "output": "3\n1\n8", "testtype": "stdin" } ], "style": "rule" }
{ "index": 38, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nMonocarp is opening his own IT company. He wants to hire $n$ programmers and $m$ testers.\n\nThere are $n+m+1$ candidates, numbered from $1$ to $n+m+1$ in chronological order of their arriving time. The $i$-th candidate has programming skill $a_i$ and testing skill $b_i$ (a person's programming skill is different from their testing skill). The skill of the team is the sum of the programming skills of all candidates hired as programmers, and the sum of the testing skills of all candidates hired as testers.\n\nWhen a candidate arrives to interview, Monocarp tries to assign them to the most suitable position for them (if their programming skill is higher, then he hires them as a programmer, otherwise as a tester). If all slots for that position are filled, Monocarp assigns them to the other position.\n\nYour task is, for each candidate, calculate the skill of the team if everyone except them comes to interview. Note that it means that exactly $n+m$ candidates will arrive, so all $n+m$ positions in the company will be filled.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nEach test case consists of three lines:\n\n * the first line contains two integers $n$ and $m$ ($0 \\le n, m \\le 2 \\cdot 10^5$; $2 \\le n + m + 1 \\le 2 \\cdot 10^5$) β€” the number of programmers and the number of testers Monocarp wants to hire, respectively; * the second line contains $n + m + 1$ integers $a_1, a_2, \\dots, a_{n+m+1}$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the programming skill of the $i$-th candidate; * the third line contains $n + m + 1$ integers $b_1, b_2, \\dots, b_{n+m+1}$ ($1 \\le b_i \\le 10^9$; $b_i \\ne a_i$), where $b_i$ is the testing skill of the $i$-th candidate. \n\nAdditional constraint on the input: the sum of $(n + m + 1)$ over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, print $n + m + 1$ integers, where the $i$-th integer should be equal to the skill of the team if everyone except the $i$-th candidate comes to interview.\n\nTags:\n['binary search', 'dp', 'greedy', 'implementation', 'two pointers']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n1 0\n2 1\n1 2\n0 2\n4 5 5\n5 4 1\n1 2\n2 1 5 4\n5 2 3 1\n3 1\n4 3 3 4 1\n5 5 4 5 2", "metadata": { "func_name": null }, "output": "1 2 \n5 6 9 \n8 11 11 12 \n13 13 13 12 15", "testtype": "stdin" } ], "style": "rule" }
{ "index": 39, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nA regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example:\n\n * bracket sequences \"()()\" and \"(())\" are regular (the resulting expressions are: \"(1)+(1)\" and \"((1+1)+1)\"); * bracket sequences \")(\", \"(\" and \")\" are not. \n\nLet's define the inverse of the bracket sequence as follows: replace all brackets '(' with ')', and vice versa (all brackets ')' with '('). For example, strings \"()((\" and \")())\" are inverses of each other.\n\nYou are given a regular bracket sequence $s$. Calculate the number of pairs of integers $(l,r)$ ($1 \\le l \\le r \\le |s|$) such that if you replace the substring of $s$ from the $l$-th character to the $r$-th character (inclusive) with its inverse, $s$ will still be a regular bracket sequence.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThe only line of each test case contains a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'.\n\nAdditional constraint on the input: the total length of the regular bracket sequences over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, print a single integer β€” the number of pairs $(l,r)$ meeting the conditions from the statement.\n\nTags:\n['binary search', 'combinatorics', 'data structures', 'divide and conquer', 'implementation', 'two pointers']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n(())\n()\n()()()\n(()())(())", "metadata": { "func_name": null }, "output": "1\n0\n3\n13", "testtype": "stdin" } ], "style": "rule" }
{ "index": 40, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nInitially, we had one array, which was a permutation of size $n$ (an array of size $n$ where each integer from $1$ to $n$ appears exactly once).\n\nWe performed $q$ operations. During the $i$-th operation, we did the following:\n\n * choose any array we have with at least $2$ elements; * split it into two non-empty arrays (prefix and suffix); * write two integers $l_i$ and $r_i$, where $l_i$ is the maximum element in the left part which we get after the split, and $r_i$ is the maximum element in the right part; * remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool. \n\nFor example, suppose the initial array was $[6, 3, 4, 1, 2, 5]$, and we performed the following operations:\n\n 1. choose the array $[6, 3, 4, 1, 2, 5]$ and split it into $[6, 3]$ and $[4, 1, 2, 5]$. Then we write $l_1 = 6$ and $r_1 = 5$, and the arrays we have are $[6, 3]$ and $[4, 1, 2, 5]$; 2. choose the array $[4, 1, 2, 5]$ and split it into $[4, 1, 2]$ and $[5]$. Then we write $l_2 = 4$ and $r_2 = 5$, and the arrays we have are $[6, 3]$, $[4, 1, 2]$ and $[5]$; 3. choose the array $[4, 1, 2]$ and split it into $[4]$ and $[1, 2]$. Then we write $l_3 = 4$ and $r_3 = 2$, and the arrays we have are $[6, 3]$, $[4]$, $[1, 2]$ and $[5]$. \n\nYou are given two integers $n$ and $q$, and two sequences $[l_1, l_2, \\dots, l_q]$ and $[r_1, r_2, \\dots, r_q]$. A permutation of size $n$ is called valid if we can perform $q$ operations and produce the given sequences $[l_1, l_2, \\dots, l_q]$ and $[r_1, r_2, \\dots, r_q]$.\n\nCalculate the number of valid permutations.\n\nInput Format:\nThe first line contains two integers $n$ and $q$ ($1 \\le q < n \\le 3 \\cdot 10^5$).\n\nThe second line contains $q$ integers $l_1, l_2, \\dots, l_q$ ($1 \\le l_i \\le n$).\n\nThe third line contains $q$ integers $r_1, r_2, \\dots, r_q$ ($1 \\le r_i \\le n$).\n\nAdditional constraint on the input: there exists at least one permutation which can produce the given sequences $[l_1, l_2, \\dots, l_q]$ and $[r_1, r_2, \\dots, r_q]$.\n\nOutput Format:\nPrint one integer β€” the number of valid permutations, taken modulo $998244353$.\n\nTags:\n['combinatorics', 'data structures', 'dfs and similar', 'greedy', 'math', 'trees']\n\nTime Limit: 2000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6 3\n6 4 4\n5 5 2", "metadata": { "func_name": null }, "output": "30", "testtype": "stdin" }, { "input": "10 1\n10\n9", "metadata": { "func_name": null }, "output": "1814400", "testtype": "stdin" }, { "input": "4 1\n2\n4", "metadata": { "func_name": null }, "output": "8", "testtype": "stdin" } ], "style": "rule" }
{ "index": 41, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given a rooted tree, consisting of $n$ vertices, numbered from $1$ to $n$. Vertex $1$ is the root. Additionally, the root only has one child.\n\nYou are asked to add exactly $k$ edges to the tree (possibly, multiple edges and/or edges already existing in the tree).\n\nRecall that a bridge is such an edge that, after you remove it, the number of connected components in the graph increases. So, initially, all edges of the tree are bridges.\n\nAfter $k$ edges are added, some original edges of the tree are still bridges and some are not anymore. You want to satisfy two conditions:\n\n * for every bridge, all tree edges in the subtree of the lower vertex of that bridge should also be bridges; * the number of bridges is as small as possible. \n\nSolve the task for all values of $k$ from $1$ to $n - 1$ and output the smallest number of bridges.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of testcases.\n\nThe first line of each testcase contains a single integer $n$ ($2 \\le n \\le 3 \\cdot 10^5$) β€” the number of vertices of the tree.\n\nEach of the next $n - 1$ lines contain two integers $v$ and $u$ ($1 \\le v, u \\le n$) β€” the description of the edges of the tree. It's guaranteed that the given edges form a valid tree.\n\nAdditional constraint on the input: the root (vertex $1$) has exactly one child.\n\nThe sum of $n$ over all testcases doesn't exceed $3 \\cdot 10^5$.\n\nOutput Format:\nFor each testcase, print $n - 1$ integers. For each $k$ from $1$ to $n - 1$ print the smallest number of bridges that can be left after you add $k$ edges to the tree.\n\nTags:\n['data structures', 'dfs and similar', 'dp', 'greedy', 'sortings', 'trees']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n2\n1 2\n12\n4 10\n5 12\n12 11\n3 6\n9 6\n1 6\n12 7\n11 6\n2 11\n10 9\n10 8\n8\n1 2\n2 3\n2 4\n3 5\n3 6\n4 7\n4 8\n5\n1 2\n2 3\n3 4\n4 5", "metadata": { "func_name": null }, "output": "0 \n7 3 1 0 0 0 0 0 0 0 0 \n4 1 0 0 0 0 0 \n0 0 0 0", "testtype": "stdin" } ], "style": "rule" }
{ "index": 42, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThe little boy Nikita was given some cubes as a present. He decided to build a tower out of them.\n\nInitially, the tower doesn't have any cubes. In one move, Nikita either puts exactly $1$ cube on top of the tower or removes exactly $1$ cube from the top of the tower. Is it possible that after $n$ moves, the resulting tower has exactly $m$ cubes?\n\nInput Format:\nEach test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 100$) β€” the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains two integers $n$ and $m$ ($1 \\le n, m \\le 100$).\n\nOutput Format:\nFor each test case, output \"Yes\" (without quotes) if Nikita can obtain a tower with $m$ cubes, and \"No\" (without quotes) otherwise.\n\nYou can output each letter in any case (lowercase or uppercase). For example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be accepted as a positive answer.\n\nTags:\n['math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n3 3\n2 4\n5 3", "metadata": { "func_name": null }, "output": "Yes\nNo\nYes", "testtype": "stdin" } ], "style": "rule" }
{ "index": 43, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given a positive integer $x$. Find any array of integers $a_0, a_1, \\ldots, a_{n-1}$ for which the following holds:\n\n * $1 \\le n \\le 32$, * $a_i$ is $1$, $0$, or $-1$ for all $0 \\le i \\le n - 1$, * $x = \\displaystyle{\\sum_{i=0}^{n - 1}{a_i \\cdot 2^i}}$, * There does not exist an index $0 \\le i \\le n - 2$ such that both $a_{i} \\neq 0$ and $a_{i + 1} \\neq 0$. \n\nIt can be proven that under the constraints of the problem, a valid array always exists.\n\nInput Format:\nEach test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains a single positive integer $x$ ($1 \\le x < 2^{30}$).\n\nOutput Format:\nFor each test case, output two lines.\n\nOn the first line, output an integer $n$ ($1 \\le n \\le 32$) β€” the length of the array $a_0, a_1, \\ldots, a_{n-1}$.\n\nOn the second line, output the array $a_0, a_1, \\ldots, a_{n-1}$.\n\nIf there are multiple valid arrays, you can output any of them.\n\nTags:\n['bitmasks', 'constructive algorithms', 'greedy', 'math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n1\n14\n24\n15\n27\n11\n19", "metadata": { "func_name": null }, "output": "1\n1\n5\n0 -1 0 0 1\n6\n0 0 0 -1 0 1\n5\n-1 0 0 0 1\n6\n-1 0 -1 0 0 1\n5\n-1 0 -1 0 1\n5\n-1 0 1 0 1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 44, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nNikita is a student passionate about number theory and algorithms. He faces an interesting problem related to an array of numbers.\n\nSuppose Nikita has an array of integers $a$ of length $n$. He will call a subsequence$^\\dagger$ of the array special if its [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple) is not contained in $a$. The LCM of an empty subsequence is equal to $0$.\n\nNikita wonders: what is the length of the longest special subsequence of $a$? Help him answer this question!\n\n$^\\dagger$ A sequence $b$ is a subsequence of $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly, zero or all) elements, without changing the order of the remaining elements. For example, $[5,2,3]$ is a subsequence of $[1,5,7,8,2,4,3]$.\n\nInput Format:\nEach test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 2000$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 2000$) β€” the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$) β€” the elements of the array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.\n\nOutput Format:\nFor each test case, output a single integer β€” the length of the longest special subsequence of $a$.\n\nTags:\n['brute force', 'data structures', 'dp', 'greedy', 'math', 'number theory', 'sortings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n5\n1 2 4 8 16\n6\n3 2 10 20 60 1\n7\n2 3 4 6 12 100003 1200036\n9\n2 42 7 3 6 7 7 1 6\n8\n4 99 57 179 10203 2 11 40812\n1\n1", "metadata": { "func_name": null }, "output": "0\n4\n4\n5\n8\n0", "testtype": "stdin" } ], "style": "rule" }
{ "index": 45, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given a binary (consisting only of 0s and 1s) $n \\times m$ matrix. You are also given a XORificator, using which you can invert all the values in a chosen row (i.e. replace 0 with 1 and 1 with 0).\n\nA column in the matrix is considered special if it contains exactly one 1. Your task is to find the maximum number of columns that can be made special at the same time, and the set of rows the XORificator should be used on to achieve that.\n\nInput Format:\nEach test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $m$ ($1 \\leq n, m \\leq 3 \\cdot 10^5$, $n \\cdot m \\leq 3 \\cdot 10^5$).\n\nEach of the following $n$ lines of the test case contains a binary string of length $m$.\n\nIt is guaranteed that the sum of $n \\cdot m$ over all test cases does not exceed $3 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output two lines.\n\nIn the first line, output the maximum number of special columns that is possible to get simultaneously.\n\nIn the second line, output a binary string of length $n$, where the $i$-th character is 0, if you don't use the XORificator on the $i$-th row, and 1, if you use the XORificator on the $i$-th row.\n\nIf there are multiple valid XORificator configurations that achieve the optimal answer, you can output any of them.\n\nTags:\n['bitmasks', 'brute force', 'greedy', 'hashing']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n3 4\n1010\n0110\n0100\n1 1\n1\n1 1\n0\n2 5\n00101\n10110\n3 3\n101\n111\n000", "metadata": { "func_name": null }, "output": "3\n010\n1\n0\n1\n1\n3\n00\n2\n010", "testtype": "stdin" } ], "style": "rule" }
{ "index": 46, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThis is an interactive problem.\n\nYou are given an integer $n$.\n\nThe jury has hidden from you a directed graph with $n$ vertices (numbered from $1$ to $n$) and some number of edges. You additionally know that:\n\n * The graph only contains edges of the form $i \\leftarrow j$, where $1 \\le i < j \\le n$. * For any three vertices $1 \\le i < j < k \\le n$, at least one of the following holds$^\\dagger$: * Vertex $i$ is reachable from vertex $j$, or * Vertex $i$ is reachable from vertex $k$, or * Vertex $j$ is reachable from vertex $k$. \n\nYou want to color each vertex in either black or white such that for any two vertices $i$ and $j$ ($1 \\le i < j \\le n$) of the same color, vertex $i$ is reachable from vertex $j$.\n\nTo do that, you can ask queries of the following type:\n\n * ? i j β€” is vertex $i$ reachable from vertex $j$ ($1 \\le i < j \\le n$)? \n\nFind any valid vertex coloring of the hidden graph in at most $2 \\cdot n$ queries. It can be proven that such a coloring always exists.\n\nNote that the grader is not adaptive: the graph is fixed before any queries are made.\n\n$^\\dagger$ Vertex $a$ is reachable from vertex $b$ if there exists a [path](https://en.wikipedia.org/wiki/Path_\\(graph_theory\\)) from vertex $b$ to vertex $a$ in the graph.\n\nInput Format:\nEach test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 1000$) β€” the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains a single integer $n$ ($3 \\le n \\le 100$) β€” the number of vertices in the hidden graph.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.\n\nOutput Format:\n\n\nTags:\n['constructive algorithms', 'graphs', 'interactive']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "2\n4\n\nYES\n\nYES\n\nYES\n\nNO\n\nNO\n\nNO\n\n5", "metadata": { "func_name": null }, "output": "? 1 2\n\n? 2 3\n\n? 1 3\n\n? 1 4\n\n? 2 4\n\n? 3 4\n\n! 0 0 0 1\n\n! 1 1 0 1 0", "testtype": "stdin" } ], "style": "rule" }
{ "index": 47, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nAlice has $n$ books. The $1$-st book contains $a_1$ pages, the $2$-nd book contains $a_2$ pages, $\\ldots$, the $n$-th book contains $a_n$ pages. Alice does the following:\n\n * She divides all the books into two non-empty piles. Thus, each book ends up in exactly one of the two piles. * Alice reads one book with the highest number in each pile.\n\nAlice loves reading very much. Help her find the maximum total number of pages she can read by dividing the books into two piles.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 500$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 100$) β€” the number of books Alice has.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$) β€” the number of pages in each book.\n\nOutput Format:\nFor each test case, output a single integer β€” the maximum number of pages Alice can read.\n\nTags:\n['constructive algorithms', 'greedy', 'sortings']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n2\n1 1\n4\n2 3 3 1\n5\n2 2 3 2 2\n2\n10 3\n3\n1 2 3", "metadata": { "func_name": null }, "output": "2\n4\n5\n13\n5", "testtype": "stdin" } ], "style": "rule" }
{ "index": 48, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nBob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion:\n\n * Bob chooses some integer $k$ ($0 \\le k \\le \\min(n, b)$). * Bob sells the first $k$ buns at a modified price. In this case, the price of the $i$-th ($1 \\le i \\le k$) sold bun is $(b - i + 1)$ coins. * The remaining $(n - k)$ buns are sold at $a$ coins each.\n\nNote that $k$ can be equal to $0$. In this case, Bob will sell all the buns at $a$ coins each.\n\nHelp Bob determine the maximum profit he can obtain by selling all $n$ buns.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains three integers $n$, $a$, and $b$ ($1 \\le n, a, b \\le 10^9$) β€” the number of buns, the usual price of a bun, and the price of the first bun to be sold at a modified price.\n\nOutput Format:\nFor each test case, output a single integer β€” the maximum profit that Bob can obtain.\n\nTags:\n['binary search', 'greedy', 'math', 'ternary search']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n4 4 5\n5 5 9\n10 10 5\n5 5 11\n1000000000 1000000000 1000000000\n1000000000 1000000000 1\n1000 1 1000", "metadata": { "func_name": null }, "output": "17\n35\n100\n45\n1000000000000000000\n1000000000000000000\n500500", "testtype": "stdin" } ], "style": "rule" }
{ "index": 49, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nLet's call the Manhattan value of a permutation$^{\\dagger}$ $p$ the value of the expression $|p_1 - 1| + |p_2 - 2| + \\ldots + |p_n - n|$.\n\nFor example, for the permutation $[1, 2, 3]$, the Manhattan value is $|1 - 1| + |2 - 2| + |3 - 3| = 0$, and for the permutation $[3, 1, 2]$, the Manhattan value is $|3 - 1| + |1 - 2| + |2 - 3| = 2 + 1 + 1 = 4$.\n\nYou are given integers $n$ and $k$. Find a permutation $p$ of length $n$ such that its Manhattan value is equal to $k$, or determine that no such permutation exists.\n\n$^{\\dagger}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\leq t \\leq 10^{4}$) β€” the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^{5}, 0 \\le k \\le 10^{12}$) β€” the length of the permutation and the required Manhattan value.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^{5}$.\n\nOutput Format:\nFor each test case, if there is no suitable permutation, output \"No\". Otherwise, in the first line, output \"Yes\", and in the second line, output $n$ distinct integers $p_1, p_2, \\ldots, p_n$ ($1 \\le p_i \\le n$) β€” a suitable permutation.\n\nIf there are multiple solutions, output any of them.\n\nYou can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be recognized as a positive answer).\n\nTags:\n['constructive algorithms', 'data structures', 'greedy', 'implementation', 'math']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "8\n3 4\n4 5\n7 0\n1 1000000000000\n8 14\n112 777\n5 12\n5 2", "metadata": { "func_name": null }, "output": "Yes\n3 1 2\nNo\nYes\n1 2 3 4 5 6 7\nNo\nYes\n8 2 3 4 5 6 1 7\nNo\nYes\n5 4 3 1 2\nYes\n2 1 3 4 5", "testtype": "stdin" } ], "style": "rule" }
{ "index": 50, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nElections are taking place in Berland. There are $n$ candidates participating in the elections, numbered from $1$ to $n$. The $i$-th candidate has $a_i$ fans who will vote for him. Additionally, there are $c$ people who are undecided about their favorite candidate, let's call them undecided. Undecided people will vote for the candidate with the lowest number.\n\nThe candidate who receives the maximum number of votes wins the elections, and if multiple candidates receive the same maximum number of votes, the candidate with the lowest number among them wins.\n\nYou found these elections too boring and predictable, so you decided to exclude some candidates from them. If you do not allow candidate number $i$ to participate in the elections, all $a_i$ of his fans will become undecided, and will vote for the candidate with the lowest number.\n\nYou are curious to find, for each $i$ from $1$ to $n$, the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win the elections.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\leq t \\leq 2 \\cdot 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $c$ ($1 \\le n \\le 2 \\cdot 10^5$, $0 \\le c \\le 10^9$) β€” the number of candidates in the elections and the number of undecided people.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^9$) β€” the number of fans for each candidate.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output $n$ integers, the $i$-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win.\n\nTags:\n['data structures', 'greedy', 'implementation', 'math']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n3 1\n2 0 3\n2 3\n0 10\n5 3\n5 4 3 2 1\n4 5\n3 10 7 1\n6 0\n2 2 2 3 3 3", "metadata": { "func_name": null }, "output": "0 1 2\n1 0\n0 1 2 3 4\n1 0 2 3\n1 1 2 0 4 5", "testtype": "stdin" } ], "style": "rule" }
{ "index": 51, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nSasha has two binary strings $s$ and $t$ of the same length $n$, consisting of the characters 0 and 1.\n\nThere is also a computing machine that can perform two types of operations on binary strings $a$ and $b$ of the same length $k$:\n\n 1. If $a_{i} = a_{i + 2} =$ 0, then you can assign $b_{i + 1} :=$ 1 ($1 \\le i \\le k - 2$). 2. If $b_{i} = b_{i + 2} =$ 1, then you can assign $a_{i + 1} :=$ 1 ($1 \\le i \\le k - 2$). \n\nSasha became interested in the following: if we consider the string $a=s_ls_{l+1}\\ldots s_r$ and the string $b=t_lt_{l+1}\\ldots t_r$, what is the maximum number of 1 characters in the string $a$ that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs $(l_i, r_i)$ that interest him.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^{4}$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) β€” the length of the strings $s$ and $t$.\n\nThe second line of each test case contains a binary string $s$ of length $n$, consisting of the characters 0 and 1.\n\nThe third line of each test case contains a binary string $t$ of length $n$, consisting of the characters 0 and 1.\n\nThe fourth line of each test case contains a single integer $q$ ($1 \\le q \\le 2 \\cdot 10^5$) β€” the number of queries.\n\nThe $i$-th of the following lines contains two integers $l_{i}$ and $r_{i}$ ($1 \\le l_{i} \\le r_{i} \\le n$) β€” the boundaries of the $i$-th pair of substrings that interest Sasha.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$ and the sum of $q$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output $q$ integers β€” the answers to all queries.\n\nTags:\n['brute force', 'data structures', 'dp', 'greedy', 'implementation']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n4\n1111\n0000\n2\n1 2\n2 4\n4\n1010\n1101\n2\n1 3\n1 4\n6\n010101\n011010\n5\n2 3\n1 6\n2 5\n4 4\n3 6", "metadata": { "func_name": null }, "output": "2\n3\n2\n3\n1\n4\n3\n1\n2", "testtype": "stdin" } ], "style": "rule" }
{ "index": 52, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nGiven an array $a$ of length $n$. Let's construct a square matrix $b$ of size $n \\times n$, in which the $i$-th row contains the array $a$ cyclically shifted to the right by $(i - 1)$. For example, for the array $a = [3, 4, 5]$, the obtained matrix is\n\n$$b = \\begin{bmatrix} 3 & 4 & 5 \\\\\\ 5 & 3 & 4 \\\\\\ 4 & 5 & 3 \\end{bmatrix}$$\n\nLet's construct the following graph:\n\n * The graph contains $n^2$ vertices, each of which corresponds to one of the elements of the matrix. Let's denote the vertex corresponding to the element $b_{i, j}$ as $(i, j)$. * We will draw an edge between vertices $(i_1, j_1)$ and $(i_2, j_2)$ if $|i_1 - i_2| + |j_1 - j_2| \\le k$ and $\\gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1$, where $\\gcd(x, y)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$. \n\nYour task is to calculate the number of connected components$^{\\dagger}$ in the obtained graph.\n\n$^{\\dagger}$A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\leq t \\leq 10^5$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $k$ ($2 \\le n \\le 10^6$, $2 \\le k \\le 2 \\cdot 10^6$) β€” the length of the array and the parameter $k$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^6$) β€” the elements of the array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.\n\nOutput Format:\nFor each test case, output a single integer β€” the number of connected components in the obtained graph.\n\nTags:\n['data structures', 'dfs and similar', 'dsu', 'graphs', 'number theory', 'two pointers']\n\nTime Limit: 4000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n3 3\n3 4 5\n3 3\n3 4 9\n3 2\n3 4 9\n2 2\n2 8\n5 3\n8 27 5 4 3\n4 10\n2 2 2 2", "metadata": { "func_name": null }, "output": "3\n2\n3\n1\n4\n1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 53, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nAlice and Bob came up with a rather strange game. They have an array of integers $a_1, a_2,\\ldots, a_n$. Alice chooses a certain integer $k$ and tells it to Bob, then the following happens:\n\n * Bob chooses two integers $i$ and $j$ ($1 \\le i < j \\le n$), and then finds the maximum among the integers $a_i, a_{i + 1},\\ldots, a_j$; * If the obtained maximum is strictly greater than $k$, Alice wins, otherwise Bob wins. \n\nHelp Alice find the maximum $k$ at which she is guaranteed to win.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 5 \\cdot 10^4$) β€” the number of elements in the array.\n\nThe second line of each test case contains $n$ integers $a_1, a_2,\\ldots, a_n$ ($1 \\le a_i \\le 10^9$) β€” the elements of the array.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $5 \\cdot 10^4$.\n\nOutput Format:\nFor each test case, output one integer β€” the maximum integer $k$ at which Alice is guaranteed to win.\n\nTags:\n['brute force', 'greedy', 'implementation']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n4\n2 4 1 7\n5\n1 2 3 4 5\n2\n1 1\n3\n37 8 16\n5\n10 10 10 10 9\n10\n3 12 9 5 2 3 2 9 8 2", "metadata": { "func_name": null }, "output": "3\n1\n0\n15\n9\n2", "testtype": "stdin" } ], "style": "rule" }
{ "index": 54, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given two distinct non-negative integers $x$ and $y$. Consider two infinite sequences $a_1, a_2, a_3, \\ldots$ and $b_1, b_2, b_3, \\ldots$, where\n\n * $a_n = n \\oplus x$; * $b_n = n \\oplus y$. \n\nHere, $x \\oplus y$ denotes the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation of integers $x$ and $y$.\n\nFor example, with $x = 6$, the first $8$ elements of sequence $a$ will look as follows: $[7, 4, 5, 2, 3, 0, 1, 14, \\ldots]$. Note that the indices of elements start with $1$.\n\nYour task is to find the length of the longest common subsegment$^\\dagger$ of sequences $a$ and $b$. In other words, find the maximum integer $m$ such that $a_i = b_j, a_{i + 1} = b_{j + 1}, \\ldots, a_{i + m - 1} = b_{j + m - 1}$ for some $i, j \\ge 1$.\n\n$^\\dagger$A subsegment of sequence $p$ is a sequence $p_l,p_{l+1},\\ldots,p_r$, where $1 \\le l \\le r$.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains two integers $x$ and $y$ ($0 \\le x, y \\le 10^9, x \\neq y$) β€” the parameters of the sequences.\n\nOutput Format:\nFor each test case, output a single integer β€” the length of the longest common subsegment.\n\nTags:\n['bitmasks', 'greedy']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n0 1\n12 4\n57 37\n316560849 14570961", "metadata": { "func_name": null }, "output": "1\n8\n4\n33554432", "testtype": "stdin" } ], "style": "rule" }
{ "index": 55, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou have been offered to play a game. In this game, there are $n$ possible outcomes, and for each of them, you must bet a certain integer amount of coins. In the event that the $i$-th outcome turns out to be winning, you will receive back the amount of coins equal to your bet on that outcome, multiplied by $k_i$. Note that exactly one of the $n$ outcomes will be winning.\n\nYour task is to determine how to distribute the coins in such a way that you will come out ahead in the event of any winning outcome. More formally, the total amount of coins you bet on all outcomes must be strictly less than the number of coins received back for each possible winning outcome.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 50$) β€” the number of outcomes.\n\nThe second line of each test case contains $n$ integers $k_1,k_2,\\ldots,k_n$ ($2 \\le k_i \\le 20$) β€” the multiplier for the amount of coins if the $i$-th outcome turns out to be winning.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output $-1$ if there is no way to distribute the coins as required. Otherwise, output $n$ integers $x_1, x_2,\\ldots, x_n$ ($1 \\le x_i \\le 10^{9}$) β€” your bets on the outcomes.\n\nIt can be shown that if a solution exists, there is always a solution that satisfies these constraints.\n\nIf there are multiple suitable solutions, output any of them.\n\nTags:\n['binary search', 'combinatorics', 'constructive algorithms', 'number theory']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n3\n3 2 7\n2\n3 3\n5\n5 5 5 5 5\n6\n7 9 3 17 9 13\n3\n6 3 2\n5\n9 4 6 8 3", "metadata": { "func_name": null }, "output": "27 41 12 \n1 1 \n-1\n1989 1547 4641 819 1547 1071 \n-1\n8 18 12 9 24", "testtype": "stdin" } ], "style": "rule" }
{ "index": 56, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou are given a binary string $s$ of length $n$, consisting of zeros and ones. You can perform the following operation exactly once:\n\n 1. Choose an integer $p$ ($1 \\le p \\le n$). 2. Reverse the substring $s_1 s_2 \\ldots s_p$. After this step, the string $s_1 s_2 \\ldots s_n$ will become $s_p s_{p-1} \\ldots s_1 s_{p+1} s_{p+2} \\ldots s_n$. 3. Then, perform a cyclic shift of the string $s$ to the left $p$ times. After this step, the initial string $s_1s_2 \\ldots s_n$ will become $s_{p+1}s_{p+2} \\ldots s_n s_p s_{p-1} \\ldots s_1$. \n\nFor example, if you apply the operation to the string 110001100110 with $p=3$, after the second step, the string will become 011001100110, and after the third step, it will become 001100110011.\n\nA string $s$ is called $k$-proper if two conditions are met:\n\n * $s_1=s_2=\\ldots=s_k$; * $s_{i+k} \\neq s_i$ for any $i$ ($1 \\le i \\le n - k$). \n\nFor example, with $k=3$, the strings 000, 111000111, and 111000 are $k$-proper, while the strings 000000, 001100, and 1110000 are not.\n\nYou are given an integer $k$, which is a divisor of $n$. Find an integer $p$ ($1 \\le p \\le n$) such that after performing the operation, the string $s$ becomes $k$-proper, or determine that it is impossible. Note that if the string is initially $k$-proper, you still need to apply exactly one operation to it.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains one integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le k \\le n$, $2 \\le n \\le 10^5$) β€” the length of the string $s$ and the value of $k$. It is guaranteed that $k$ is a divisor of $n$.\n\nThe second line of each test case contains a binary string $s$ of length $n$, consisting of the characters 0 and 1.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output a single integer β€” the value of $p$ to make the string $k$-proper, or $-1$ if it is impossible.\n\nIf there are multiple solutions, output any of them.\n\nTags:\n['bitmasks', 'brute force', 'constructive algorithms', 'dp', 'greedy', 'hashing', 'strings']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n8 4\n11100001\n4 2\n1110\n12 3\n111000100011\n5 5\n00000\n6 1\n101001\n8 4\n01110001\n12 2\n110001100110", "metadata": { "func_name": null }, "output": "3\n-1\n7\n5\n4\n-1\n3", "testtype": "stdin" } ], "style": "rule" }
{ "index": 57, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThe Manhattan distance between two points $(x_1, y_1)$ and $(x_2, y_2)$ is defined as: $$|x_1 - x_2| + |y_1 - y_2|.$$\n\nWe call a Manhattan triangle three points on the plane, the Manhattan distances between each pair of which are equal.\n\nYou are given a set of pairwise distinct points and an even integer $d$. Your task is to find any Manhattan triangle, composed of three distinct points from the given set, where the Manhattan distance between any pair of vertices is equal to $d$.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains one integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $d$ ($3 \\le n \\le 2 \\cdot 10^5$, $2 \\le d \\le 4 \\cdot 10^5$, $d$ is even) β€” the number of points and the required Manhattan distance between the vertices of the triangle.\n\nThe $(i + 1)$-th line of each test case contains two integers $x_i$ and $y_i$ ($-10^5 \\le x_i, y_i \\le 10^5$) β€” the coordinates of the $i$-th point. It is guaranteed that all points are pairwise distinct.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output three distinct integers $i$, $j$, and $k$ ($1 \\le i,j,k \\le n$) β€” the indices of the points forming the Manhattan triangle. If there is no solution, output \"$0\\ 0\\ 0$\" (without quotes).\n\nIf there are multiple solutions, output any of them.\n\nTags:\n['binary search', 'constructive algorithms', 'data structures', 'geometry', 'implementation', 'two pointers']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n6 4\n3 1\n0 0\n0 -2\n5 -3\n3 -5\n2 -2\n5 4\n0 0\n0 -2\n5 -3\n3 -5\n2 -2\n6 6\n3 1\n0 0\n0 -2\n5 -3\n3 -5\n2 -2\n4 4\n3 0\n0 3\n-3 0\n0 -3\n10 8\n2 1\n-5 -1\n-4 -1\n-5 -3\n0 1\n-2 5\n-4 4\n-4 2\n0 0\n-4 1\n4 400000\n100000 100000\n-100000 100000\n100000 -100000\n-100000 -100000", "metadata": { "func_name": null }, "output": "2 6 1\n4 3 5\n3 5 1\n0 0 0\n6 1 3\n0 0 0", "testtype": "stdin" } ], "style": "rule" }
{ "index": 58, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThis is an interactive problem.\n\nKostyanych has chosen a complete undirected graph$^{\\dagger}$ with $n$ vertices, and then removed exactly $(n - 2)$ edges from it. You can ask queries of the following type:\n\n * \"? $d$\" β€” Kostyanych tells you the number of vertex $v$ with a degree at least $d$. Among all possible such vertices, he selects the vertex with the minimum degree, and if there are several such vertices, he selects the one with the minimum number. He also tells you the number of another vertex in the graph, with which $v$ is not connected by an edge (if none is found, then $0$ is reported). Among all possible such vertices, he selects the one with the minimum number. Then he removes the vertex $v$ and all edges coming out of it. If the required vertex $v$ is not found, then \"$0\\ 0$\" is reported. \n\nFind a Hamiltonian path$^{\\ddagger}$ in the original graph in at most $n$ queries. It can be proven that under these constraints, a Hamiltonian path always exists.\n\n$^{\\dagger}$A complete undirected graph is a graph in which there is exactly one undirected edge between any pair of distinct vertices. Thus, a complete undirected graph with $n$ vertices contains $\\frac{n(n-1)}{2}$ edges.\n\n$^{\\ddagger}$A Hamiltonian path in a graph is a path that passes through each vertex exactly once.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 1000$) β€” the number of test cases. The description of the test cases follows.\n\nThe only line of each test case contains a single integer $n$ ($2 \\le n \\le 10^5$) β€” the number of vertices in the graph.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput Format:\n\n\nTags:\n['brute force', 'constructive algorithms', 'graphs', 'interactive']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n4\n\n0 0\n\n1 4\n\n2 3\n\n4\n\n1 0\n\n4 2\n\n2\n\n1 0", "metadata": { "func_name": null }, "output": "? 3\n\n? 2\n\n? 1\n\n! 4 3 1 2\n\n? 3\n\n? 0\n\n! 4 1 2 3\n\n? 0\n\n! 2 1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 59, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nVlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.\n\nVlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a_i$. There may not be enough of these problems, so he may have to come up with a few more problems.\n\nVlad wants to come up with as few problems as possible, so he asks you to find the minimum number of problems he needs to come up with in order to hold $m$ rounds.\n\nFor example, if $m=1$, $n = 10$, $a=$ 'BGECDCBDED', then he needs to come up with two problems: one of difficulty level 'A' and one of difficulty level 'F'.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 1000$) β€” the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $m$ ($1 \\le n \\le 50$, $1 \\le m \\le 5$) β€” the number of problems in the bank and the number of upcoming rounds, respectively.\n\nThe second line of each test case contains a string $a$ of $n$ characters from 'A' to 'G' β€” the difficulties of the problems in the bank.\n\nOutput Format:\nFor each test case, output a single integer β€” the minimum number of problems that need to come up with to hold $m$ rounds.\n\nTags:\n['math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n10 1\nBGECDCBDED\n10 2\nBGECDCBDED\n9 1\nBBCDEFFGG", "metadata": { "func_name": null }, "output": "2\n5\n1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 60, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nDmitry has $n$ cubes, numbered from left to right from $1$ to $n$. The cube with index $f$ is his favorite.\n\nDmitry threw all the cubes on the table, and the $i$-th cube showed the value $a_i$ ($1 \\le a_i \\le 100$). After that, he arranged the cubes in non-increasing order of their values, from largest to smallest. If two cubes show the same value, they can go in any order.\n\nAfter sorting, Dmitry removed the first $k$ cubes. Then he became interested in whether he removed his favorite cube (note that its position could have changed after sorting).\n\nFor example, if $n=5$, $f=2$, $a = [4, \\color{green}3, 3, 2, 3]$ (the favorite cube is highlighted in green), and $k = 2$, the following could have happened:\n\n * After sorting $a=[4, \\color{green}3, 3, 3, 2]$, since the favorite cube ended up in the second position, it will be removed. * After sorting $a=[4, 3, \\color{green}3, 3, 2]$, since the favorite cube ended up in the third position, it will not be removed.\n\nInput Format:\nThe first line contains an integer $t$ ($1 \\le t \\le 1000$) β€” the number of test cases. Then follow the descriptions of the test cases.\n\nThe first line of each test case description contains three integers $n$, $f$, and $k$ ($1 \\le f, k \\le n \\le 100$) β€” the number of cubes, the index of Dmitry's favorite cube, and the number of removed cubes, respectively.\n\nThe second line of each test case description contains $n$ integers $a_i$ ($1 \\le a_i \\le 100$) β€” the values shown on the cubes.\n\nOutput Format:\nFor each test case, output one line β€” \"YES\" if the cube will be removed in all cases, \"NO\" if it will not be removed in any case, \"MAYBE\" if it may be either removed or left.\n\nYou can output the answer in any case. For example, the strings \"YES\", \"nO\", \"mAyBe\" will be accepted as answers.\n\nTags:\n['sortings', 'sortings']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "12\n5 2 2\n4 3 3 2 3\n5 5 3\n4 2 1 3 5\n5 5 2\n5 2 4 1 3\n5 5 5\n1 2 5 4 3\n5 5 4\n3 1 2 4 5\n5 5 5\n4 3 2 1 5\n6 5 3\n1 2 3 1 2 3\n10 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1\n42\n5 2 3\n2 2 1 1 2\n2 1 1\n2 1\n5 3 1\n3 3 2 3 2", "metadata": { "func_name": null }, "output": "MAYBE\nYES\nNO\nYES\nYES\nYES\nMAYBE\nMAYBE\nYES\nYES\nYES\nNO", "testtype": "stdin" } ], "style": "rule" }
{ "index": 61, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nSofia had an array of $n$ integers $a_1, a_2, \\ldots, a_n$. One day she got bored with it, so she decided to sequentially apply $m$ modification operations to it.\n\nEach modification operation is described by a pair of numbers $\\langle c_j, d_j \\rangle$ and means that the element of the array with index $c_j$ should be assigned the value $d_j$, i.e., perform the assignment $a_{c_j} = d_j$. After applying all modification operations sequentially, Sofia discarded the resulting array.\n\nRecently, you found an array of $n$ integers $b_1, b_2, \\ldots, b_n$. You are interested in whether this array is Sofia's array. You know the values of the original array, as well as the values $d_1, d_2, \\ldots, d_m$. The values $c_1, c_2, \\ldots, c_m$ turned out to be lost.\n\nIs there a sequence $c_1, c_2, \\ldots, c_m$ such that the sequential application of modification operations $\\langle c_1, d_1, \\rangle, \\langle c_2, d_2, \\rangle, \\ldots, \\langle c_m, d_m \\rangle$ to the array $a_1, a_2, \\ldots, a_n$ transforms it into the array $b_1, b_2, \\ldots, b_n$?\n\nInput Format:\nThe first line contains an integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThen follow the descriptions of the test cases.\n\nThe first line of each test case contains an integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) β€” the size of the array.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$) β€” the elements of the original array.\n\nThe third line of each test case contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le 10^9$) β€” the elements of the found array.\n\nThe fourth line contains an integer $m$ ($1 \\le m \\le 2 \\cdot 10^5$) β€” the number of modification operations.\n\nThe fifth line contains $m$ integers $d_1, d_2, \\ldots, d_m$ ($1 \\le d_j \\le 10^9$) β€” the preserved value for each modification operation.\n\nIt is guaranteed that the sum of the values of $n$ for all test cases does not exceed $2 \\cdot 10^5$, similarly the sum of the values of $m$ for all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nOutput $t$ lines, each of which is the answer to the corresponding test case. As an answer, output \"YES\" if there exists a suitable sequence $c_1, c_2, \\ldots, c_m$, and \"NO\" otherwise.\n\nYou can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).\n\nTags:\n['constructive algorithms', 'greedy']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n3\n1 2 1\n1 3 2\n4\n1 3 1 2\n4\n1 2 3 5\n2 1 3 5\n2\n2 3\n5\n7 6 1 10 10\n3 6 1 11 11\n3\n4 3 11\n4\n3 1 7 8\n2 2 7 10\n5\n10 3 2 2 1\n5\n5 7 1 7 9\n4 10 1 2 9\n8\n1 1 9 8 7 2 10 4\n4\n1000000000 203 203 203\n203 1000000000 203 1000000000\n2\n203 1000000000\n1\n1\n1\n5\n1 3 4 5 1", "metadata": { "func_name": null }, "output": "YES\nNO\nNO\nNO\nYES\nNO\nYES", "testtype": "stdin" } ], "style": "rule" }
{ "index": 62, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nGCD (Greatest Common Divisor) of two integers $x$ and $y$ is the maximum integer $z$ by which both $x$ and $y$ are divisible. For example, $GCD(36, 48) = 12$, $GCD(5, 10) = 5$, and $GCD(7,11) = 1$.\n\nKristina has an array $a$ consisting of exactly $n$ positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array $b$, called GCD-sequence.\n\nSo, the elements of the GCD-sequence $b$ will be calculated using the formula $b_i = GCD(a_i, a_{i + 1})$ for $1 \\le i \\le n - 1$.\n\nDetermine whether it is possible to remove exactly one number from the array $a$ so that the GCD sequence $b$ is non-decreasing (i.e., $b_i \\le b_{i+1}$ is always true).\n\nFor example, let Khristina had an array $a$ = [$20, 6, 12, 3, 48, 36$]. If she removes $a_4 = 3$ from it and counts the GCD-sequence of $b$, she gets:\n\n * $b_1 = GCD(20, 6) = 2$ * $b_2 = GCD(6, 12) = 6$ * $b_3 = GCD(12, 48) = 12$ * $b_4 = GCD(48, 36) = 12$ \n\nThe resulting GCD sequence $b$ = [$2,6,12,12$] is non-decreasing because $b_1 \\le b_2 \\le b_3 \\le b_4$.\n\nInput Format:\nThe first line of input data contains a single number $t$ ($1 \\le t \\le 10^4$) β€” he number of test cases in the test.\n\nThis is followed by the descriptions of the test cases.\n\nThe first line of each test case contains a single integer $n$ ($3 \\le n \\le 2 \\cdot 10^5$) β€” the number of elements in the array $a$.\n\nThe second line of each test case contains exactly $n$ integers $a_i$ ($1 \\le a_i \\le 10^9$) β€” the elements of array $a$.\n\nIt is guaranteed that the sum of $n$ over all test case does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output a single line:\n\n * \"YES\" if you can remove exactly one number from the array $a$ so that the GCD-sequence of $b$ is non-decreasing; * \"NO\" otherwise. \n\nYou can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will all be recognized as a positive answer).\n\nTags:\n['greedy', 'implementation', 'math', 'number theory']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "12\n6\n20 6 12 3 48 36\n4\n12 6 3 4\n3\n10 12 3\n5\n32 16 8 4 2\n5\n100 50 2 10 20\n4\n2 4 8 1\n10\n7 4 6 2 4 5 1 4 2 8\n7\n5 9 6 8 5 9 2\n6\n11 14 8 12 9 3\n9\n5 7 3 10 6 3 12 6 3\n3\n4 2 4\n8\n1 6 11 12 6 12 3 6", "metadata": { "func_name": null }, "output": "YES\nNO\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES", "testtype": "stdin" } ], "style": "rule" }
{ "index": 63, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou have been given a matrix $a$ of size $n$ by $m$, containing a permutation of integers from $1$ to $n \\cdot m$.\n\nA permutation of $n$ integers is an array containing all numbers from $1$ to $n$ exactly once. For example, the arrays $[1]$, $[2, 1, 3]$, $[5, 4, 3, 2, 1]$ are permutations, while the arrays $[1, 1]$, $[100]$, $[1, 2, 4, 5]$ are not.\n\nA matrix contains a permutation if, when all its elements are written out, the resulting array is a permutation. Matrices $[[1, 2], [3, 4]]$, $[[1]]$, $[[1, 5, 3], [2, 6, 4]]$ contain permutations, while matrices $[[2]]$, $[[1, 1], [2, 2]]$, $[[1, 2], [100, 200]]$ do not.\n\nYou can perform one of the following two actions in one operation:\n\n * choose columns $c$ and $d$ ($1 \\le c, d \\le m$, $c \\ne d$) and swap these columns; * choose rows $c$ and $d$ ($1 \\le c, d \\le n$, $c \\ne d$) and swap these rows. \n\nYou can perform any number of operations.\n\nYou are given the original matrix $a$ and the matrix $b$. Your task is to determine whether it is possible to transform matrix $a$ into matrix $b$ using the given operations.\n\nInput Format:\nThe first line contains an integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases. The descriptions of the test cases follow.\n\nThe first line of each test case description contains $2$ integers $n$ and $m$ ($1 \\le n, m \\le n \\cdot m \\le 2 \\cdot 10^5$) β€” the sizes of the matrix.\n\nThe next $n$ lines contain $m$ integers $a_{ij}$ each ($1 \\le a_{ij} \\le n \\cdot m$). It is guaranteed that matrix $a$ is a permutation.\n\nThe next $n$ lines contain $m$ integers $b_{ij}$ each ($1 \\le b_{ij} \\le n \\cdot m$). It is guaranteed that matrix $b$ is a permutation.\n\nIt is guaranteed that the sum of the values $n \\cdot m$ for all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output \"YES\" if the second matrix can be obtained from the first, and \"NO\" otherwise.\n\nYou can output each letter in any case (lowercase or uppercase). For example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be accepted as a positive answer.\n\nTags:\n['constructive algorithms', 'data structures', 'greedy', 'hashing', 'implementation', 'math', 'matrices', 'sortings']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "7\n1 1\n1\n1\n2 2\n1 2\n3 4\n4 3\n2 1\n2 2\n1 2\n3 4\n4 3\n1 2\n3 4\n1 5 9 6\n12 10 4 8\n7 11 3 2\n1 5 9 6\n12 10 4 8\n7 11 3 2\n3 3\n1 5 9\n6 4 2\n3 8 7\n9 5 1\n2 4 6\n7 8 3\n2 3\n1 2 6\n5 4 3\n6 1 2\n3 4 5\n1 5\n5 1 2 3 4\n4 2 5 1 3", "metadata": { "func_name": null }, "output": "YES\nYES\nNO\nYES\nYES\nNO\nYES", "testtype": "stdin" } ], "style": "rule" }
{ "index": 64, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThis is an easy version of the problem; it differs from the hard version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.\n\nAlice and Bob are dividing the field. The field is a rectangle of size $n \\times m$ ($2 \\le n, m \\le 10^9$), the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$).\n\nBob has $k$ ($2 \\le k \\le 2 \\cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions:\n\n * To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field. * Alice's path will divide the field into two parts β€” one part will belong to Alice (this part includes the cells of her path), the other part β€” to Bob. * Alice will own the part that includes the cell ($n, 1$). * Bob will own the part that includes the cell ($1, m$). \n\nAlice wants to divide the field in such a way as to get as many cells as possible.\n\nBob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\\alpha$ β€” the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot). Then output $k$ non-negative integers $a_1, a_2, \\dots, a_k$, where:\n\n * $a_i=0$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot does not increase (i.e., remains equal to $\\alpha$); * $a_i=1$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot increases (i.e., becomes greater than $\\alpha$).\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains three integers $n$, $m$, and $k$ ($2 \\le n, m \\le 10^9$, $2 \\le k \\le 2 \\cdot 10^5$) β€” the field sizes and the number of fountains, respectively.\n\nThen follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \\le r_i \\le n$, $1 \\le c_i \\le m$) β€” the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$).\n\nIt is guaranteed that the sum of $k$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, first output $\\alpha$ β€” the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \\dots, a_k$, where:\n\n * $a_i=0$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot does not increase compared to the case when all $k$ fountains belong to Bob; * $a_i=1$, if after Bob gives Alice the $i$-th fountain, the maximum possible size of Alice's plot increases compared to the case when all $k$ fountains belong to Bob. \n\nIf you output any other positive number instead of $1$ that fits into a 64-bit signed integer type, it will also be recognized as $1$. Thus, a solution to the hard version of this problem will also pass the tests for the easy version.\n\nTags:\n['data structures', 'math', 'sortings']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n2 2 3\n1 1\n1 2\n2 2\n5 5 4\n1 2\n2 2\n3 4\n4 3\n2 5 9\n1 2\n1 5\n1 1\n2 2\n2 4\n2 5\n1 4\n2 3\n1 3\n6 4 4\n6 2\n1 3\n1 4\n1 2\n3 4 5\n2 1\n3 2\n1 4\n1 3\n2 4", "metadata": { "func_name": null }, "output": "1\n1 0 1 \n11\n0 1 0 1 \n1\n0 0 1 1 0 0 0 0 0 \n6\n1 0 0 0 \n1\n1 1 0 0 0", "testtype": "stdin" } ], "style": "rule" }
{ "index": 65, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThis is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.\n\nAlice and Bob are dividing the field. The field is a rectangle of size $n \\times m$ ($2 \\le n, m \\le 10^9$); the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$).\n\nBob has $k$ ($2 \\le k \\le 2 \\cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions:\n\n * To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field. * Alice's path will divide the field into two parts β€” one part will belong to Alice (this part includes the cells of her path), the other part β€” to Bob. * Alice will own the part that includes the cell ($n, 1$). * Bob will own the part that includes the cell ($1, m$). \n\nAlice wants to divide the field in such a way as to get as many cells as possible.\n\nBob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\\alpha$ β€” the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot).\n\nThen output $k$ non-negative integers $a_1, a_2, \\dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\\alpha + a_i$.\n\nInput Format:\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThe first line of each test case contains three integers $n$, $m$, and $k$ ($2 \\le n, m \\le 10^9$, $2 \\le k \\le 2 \\cdot 10^5$) β€” the field sizes and the number of fountains, respectively.\n\nThen follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \\le r_i \\le n$, $1 \\le c_i \\le m$) β€” the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$).\n\nIt is guaranteed that the sum of $k$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, first output $\\alpha$ β€” the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \\dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\\alpha + a_i$.\n\nTags:\n['math', 'sortings']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n2 2 3\n1 1\n1 2\n2 2\n5 5 4\n1 2\n2 2\n3 4\n4 3\n2 5 9\n1 2\n1 5\n1 1\n2 2\n2 4\n2 5\n1 4\n2 3\n1 3\n6 4 4\n6 2\n1 3\n1 4\n1 2\n3 4 5\n2 1\n3 2\n1 4\n1 3\n2 4", "metadata": { "func_name": null }, "output": "1\n1 0 1 \n11\n0 1 0 4 \n1\n0 0 1 1 0 0 0 0 0 \n6\n15 0 0 0 \n1\n2 3 0 0 0", "testtype": "stdin" } ], "style": "rule" }
{ "index": 66, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYasya was walking in the forest and accidentally found a tree with $n$ vertices. A tree is a connected undirected graph with no cycles.\n\nNext to the tree, the girl found an ancient manuscript with $m$ queries written on it. The queries can be of two types.\n\nThe first type of query is described by the integer $y$. The weight of each edge in the tree is replaced by the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of the weight of that edge and the integer $y$.\n\nThe second type is described by the vertex $v$ and the integer $x$. Yasya chooses a vertex $u$ ($1 \\le u \\le n$, $u \\neq v$) and mentally draws a bidirectional edge of weight $x$ from $v$ to $u$ in the tree.\n\nThen Yasya finds a simple cycle in the resulting graph and calculates the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of all the edges in it. She wants to choose a vertex $u$ such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of $u$). If an edge between $v$ and $u$ already existed, a simple cycle is the path $v \\to u \\to v$.\n\nNote that the second type of query is performed mentally, meaning the tree does not change in any way after it.\n\nHelp Yasya answer all the queries.\n\nInput Format:\nThe first line contains an integer $t$ ($1 \\le t \\le 10^4$) β€” the number of test cases.\n\nThe descriptions of the test cases follow.\n\nThe first line of each test case contains two integers $n$, $m$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le m \\le 2 \\cdot 10^5$) β€” the number of vertices in the tree and the number of queries.\n\nThe next $n - 1$ lines of each test case contain three integers $v$, $u$, $w$ ($1 \\le v, u \\le n$, $1 \\le w \\le 10^9$) β€” the ends of some edge in the tree and its weight.\n\nIt is guaranteed that the given set of edges forms a tree.\n\nThe next $m$ lines of each test case describe the queries:\n\n * ^ $y$ ($1 \\le y \\le 10^9$) β€” parameter of the first type query; * ? $v$ $x$ ($1 \\le v \\le n$, $1 \\le x \\le 10^9$) β€” parameters of the second type query. \n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$. The same is guaranteed for $m$.\n\nOutput Format:\nFor each test case, output the answers to the queries of the second type.\n\nTags:\n['bitmasks', 'data structures', 'dfs and similar', 'graphs', 'greedy', 'strings', 'trees']\n\nTime Limit: 2500 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "2\n3 7\n1 2 1\n3 1 8\n^ 5\n? 2 9\n^ 1\n? 1 10\n^ 6\n? 3 1\n? 2 9\n5 6\n1 2 777\n3 2 2812\n4 1 16\n5 3 1000000000\n^ 4\n? 3 123\n? 5 1000000000\n^ 1000000000\n? 1 908070\n? 2 1", "metadata": { "func_name": null }, "output": "13 15 11 10 \n1000000127 2812 999756331 999999756", "testtype": "stdin" }, { "input": "3\n8 4\n8 6 3\n6 3 4\n2 5 4\n7 6 2\n7 1 10\n4 1 4\n5 1 2\n^ 4\n^ 7\n? 7 8\n? 4 10\n5 6\n3 1 4\n2 3 9\n4 3 6\n5 2 10\n? 5 7\n^ 1\n^ 8\n? 4 10\n? 1 9\n? 3 6\n4 2\n2 1 4\n4 3 5\n2 3 4\n^ 13\n? 1 10", "metadata": { "func_name": null }, "output": "14 13 \n13 8 11 11 \n10", "testtype": "stdin" } ], "style": "rule" }
{ "index": 67, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nTurtle and Piggy are playing a number game.\n\nFirst, Turtle will choose an integer $x$, such that $l \\le x \\le r$, where $l, r$ are given. It's also guaranteed that $2l \\le r$.\n\nThen, Piggy will keep doing the following operation until $x$ becomes $1$:\n\n * Choose an integer $p$ such that $p \\ge 2$ and $p \\mid x$ (i.e. $x$ is a multiple of $p$). * Set $x$ to $\\frac{x}{p}$, and the score will increase by $1$. \n\nThe score is initially $0$. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains two integers $l, r$ ($1 \\le l \\le r \\le 10^9, 2l \\le r$) β€” The range where Turtle can choose the integer from.\n\nOutput Format:\nFor each test case, output a single integer β€” the maximum score.\n\nTags:\n['brute force', 'greedy', 'math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n2 4\n3 6\n2 15\n6 22\n114514 1919810", "metadata": { "func_name": null }, "output": "2\n2\n3\n4\n20", "testtype": "stdin" } ], "style": "rule" }
{ "index": 68, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nThere is a sequence $a_0, a_1, a_2, \\ldots$ of infinite length. Initially $a_i = i$ for every non-negative integer $i$.\n\nAfter every second, each element of the sequence will simultaneously change. $a_i$ will change to $a_{i - 1} \\mid a_i \\mid a_{i + 1}$ for every positive integer $i$. $a_0$ will change to $a_0 \\mid a_1$. Here, $|$ denotes [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR).\n\nTurtle is asked to find the value of $a_n$ after $m$ seconds. In particular, if $m = 0$, then he needs to find the initial value of $a_n$. He is tired of calculating so many values, so please help him!\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains two integers $n, m$ ($0 \\le n, m \\le 10^9$).\n\nOutput Format:\nFor each test case, output a single integer β€” the value of $a_n$ after $m$ seconds.\n\nTags:\n['bitmasks', 'math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "9\n0 0\n0 1\n0 2\n1 0\n5 2\n10 1\n20 3\n1145 14\n19198 10", "metadata": { "func_name": null }, "output": "0\n1\n3\n1\n7\n11\n23\n1279\n19455", "testtype": "stdin" } ], "style": "rule" }
{ "index": 69, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nTurtle was playing with a sequence $a_1, a_2, \\ldots, a_n$ consisting of positive integers. Unfortunately, some of the integers went missing while playing.\n\nNow the sequence becomes incomplete. There may exist an arbitrary number of indices $i$ such that $a_i$ becomes $-1$. Let the new sequence be $a'$.\n\nTurtle is sad. But Turtle remembers that for every integer $i$ from $1$ to $n - 1$, either $a_i = \\left\\lfloor\\frac{a_{i + 1}}{2}\\right\\rfloor$ or $a_{i + 1} = \\left\\lfloor\\frac{a_i}{2}\\right\\rfloor$ holds for the original sequence $a$.\n\nTurtle wants you to help him complete the sequence. But sometimes Turtle makes mistakes, so you need to tell him if you can't complete the sequence.\n\nFormally, you need to find another sequence $b_1, b_2, \\ldots, b_n$ consisting of positive integers such that:\n\n * For every integer $i$ from $1$ to $n$, if $a'_i \\ne -1$, then $b_i = a'_i$. * For every integer $i$ from $1$ to $n - 1$, either $b_i = \\left\\lfloor\\frac{b_{i + 1}}{2}\\right\\rfloor$ or $b_{i + 1} = \\left\\lfloor\\frac{b_i}{2}\\right\\rfloor$ holds. * For every integer $i$ from $1$ to $n$, $1 \\le b_i \\le 10^9$. \n\nIf there is no sequence $b_1, b_2, \\ldots, b_n$ that satisfies all of the conditions above, you need to report $-1$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^5$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) β€” the length of the sequence.\n\nThe second line of each test case contains $n$ integers $a'_1, a'_2, \\ldots, a'_n$ ($a'_i = -1$ or $1 \\le a'_i \\le 10^8$) β€” the elements of the sequence $a'$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, if there is no sequence $b_1, b_2, \\ldots, b_n$ that satisfies all of the conditions, output a single integer $-1$.\n\nOtherwise, output $n$ integers $b_1, b_2, \\ldots, b_n$ β€” the elements of the sequence $b_1, b_2, \\ldots, b_n$ you find. The sequence should satisfy that $1 \\le b_i \\le 10^9$ for every integer $i$ from $1$ to $n$. If there are multiple answers, print any of them.\n\nTags:\n['bitmasks', 'brute force', 'constructive algorithms', 'greedy', 'implementation', 'math']\n\nTime Limit: 3000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "9\n8\n-1 -1 -1 2 -1 -1 1 -1\n4\n-1 -1 -1 -1\n6\n3 -1 -1 -1 9 -1\n4\n-1 5 -1 6\n4\n2 -1 -1 3\n4\n1 2 3 4\n2\n4 2\n5\n-1 3 -1 3 6\n13\n-1 -1 3 -1 -1 -1 -1 7 -1 -1 3 -1 -1", "metadata": { "func_name": null }, "output": "4 9 4 2 4 2 1 2\n7 3 6 13\n3 1 2 4 9 18\n-1\n-1\n-1\n4 2\n6 3 1 3 6\n3 1 3 1 3 7 3 7 3 1 3 1 3", "testtype": "stdin" } ], "style": "rule" }
{ "index": 70, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nTurtle just learned how to multiply two integers in his math class, and he was very excited.\n\nThen Piggy gave him an integer $n$, and asked him to construct a sequence $a_1, a_2, \\ldots, a_n$ consisting of integers which satisfied the following conditions:\n\n * For all $1 \\le i \\le n$, $1 \\le a_i \\le 3 \\cdot 10^5$. * For all $1 \\le i < j \\le n - 1$, $a_i \\cdot a_{i + 1} \\ne a_j \\cdot a_{j + 1}$. \n\nOf all such sequences, Piggy asked Turtle to find the one with the minimum number of distinct elements.\n\nTurtle definitely could not solve the problem, so please help him!\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 10^6$) β€” the length of the sequence $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.\n\nOutput Format:\nFor each test case, output $n$ integers $a_1, a_2, \\ldots, a_n$ β€” the elements of the sequence $a$.\n\nIf there are multiple answers, print any of them.\n\nTags:\n['constructive algorithms', 'dfs and similar', 'graphs', 'number theory']\n\nTime Limit: 3000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n2\n3\n4", "metadata": { "func_name": null }, "output": "114514 114514\n1 2 2\n3 3 4 4", "testtype": "stdin" } ], "style": "rule" }
{ "index": 71, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nTurtle just received $n$ segments and a sequence $a_1, a_2, \\ldots, a_n$. The $i$-th segment is $[l_i, r_i]$.\n\nTurtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undirected edge between $i$ and $j$ with a weight of $|a_i - a_j|$, for every $i \\ne j$.\n\nTurtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph $G$, or report that the graph $G$ has no spanning tree.\n\nWe say two segments $[l_1, r_1]$ and $[l_2, r_2]$ intersect if and only if $\\max(l_1, l_2) \\le \\min(r_1, r_2)$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^5$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 5 \\cdot 10^5$) β€” the number of segments.\n\nThe $i$-th of the following $n$ lines contains three integers $l_i, r_i, a_i$ ($1 \\le l_i \\le r_i \\le 10^9, 1 \\le a_i \\le 10^9$) β€” the $i$-th segment and the $i$-th element of the sequence.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $5 \\cdot 10^5$.\n\nOutput Format:\nFor each test case, output a single integer β€” the sum of the weights of the edges of the minimum spanning tree of the graph $G$. If the graph $G$ has no spanning tree, output $-1$.\n\nTags:\n['data structures', 'dsu', 'graphs', 'greedy']\n\nTime Limit: 5000 ms\nMemory Limit: 512 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "4\n5\n1 7 3\n2 4 6\n3 5 5\n6 7 9\n3 4 4\n5\n2 7 3\n1 3 6\n4 5 5\n6 7 9\n1 1 4\n4\n1 4 3\n1 2 1\n3 4 5\n1 4 4\n3\n1 3 1\n2 3 3\n4 5 8", "metadata": { "func_name": null }, "output": "9\n13\n4\n-1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 72, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nNote the unusual definition of $\\text{MEX}$ in this problem.\n\nPiggy gave Turtle a binary tree$^{\\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \\ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$.\n\nIf a set of paths $P = \\\\{(x_i, y_i)\\\\}$ in the tree covers each edge exactly once, then Turtle will think that the set of paths is good. Note that a good set of paths can cover a vertex twice or more.\n\nTurtle defines the value of a set of paths as $\\sum\\limits_{(x, y) \\in P} f(x, y)$, where $f(x, y)$ denotes the $\\text{MEX}^{\\ddagger}$ of all $a_u$ such that vertex $u$ is on the simple path from $x$ to $y$ in the tree (including the starting vertex $x$ and the ending vertex $y$).\n\nTurtle wonders the minimum value over all good sets of paths. Please help him calculate the answer!\n\n$^{\\dagger}$A binary tree is a tree where every non-leaf vertex has at most $2$ sons.\n\n$^{\\ddagger}\\text{MEX}$ of a collection of integers $c_1, c_2, \\ldots, c_k$ is defined as the smallest positive integer $x$ which does not occur in the collection $c$. For example, $\\text{MEX}$ of $[3, 3, 1, 4]$ is $2$, $\\text{MEX}$ of $[2, 3]$ is $1$.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 2.5 \\cdot 10^4$) β€” the number of vertices in the tree.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$) β€” the elements of the sequence $a$.\n\nThe third line of each test case contains $n - 1$ integers $p_2, p_3, \\ldots, p_n$ ($1 \\le p_i < i$) β€” the parent of each vertex in the tree.\n\nAdditional constraint on the input: the given tree is a binary tree, that is, every non-leaf vertex has at most $2$ sons.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput Format:\nFor each test case, output a single integer β€” the minimum value over all good sets of paths.\n\nTags:\n['data structures', 'dp', 'trees']\n\nTime Limit: 4000 ms\nMemory Limit: 1024 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "5\n5\n3 2 2 1 1\n1 1 2 2\n5\n3 2 1 1 1\n1 1 2 2\n6\n1 2 1 2 1 3\n1 2 3 3 4\n7\n2 1 2 3 1 2 1\n1 1 2 2 3 3\n10\n1 2 2 1 4 2 3 1 2 1\n1 1 2 2 3 3 4 5 5", "metadata": { "func_name": null }, "output": "4\n6\n6\n6\n7", "testtype": "stdin" } ], "style": "rule" }
{ "index": 73, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nDima loves watching soccer. In such a game, the score on the scoreboard is represented as $x$ : $y$, where $x$ is the number of goals of the first team, and $y$ is the number of goals of the second team. At any given time, only one team can score a goal, so the score $x$ : $y$ can change to either $(x + 1)$ : $y$, or $x$ : $(y + 1)$.\n\nWhile watching a soccer game, Dima was distracted by very important matters, and after some time, he returned to watching the game. Dima remembers the score right before he was distracted, and the score right after he returned. Given these two scores, he wonders the following question. Is it possible that, while Dima was not watching the game, the teams never had an equal score?\n\nIt is guaranteed that at neither of the two time points Dima remembers the teams had equal scores. However, it is possible that the score did not change during his absence.\n\nHelp Dima and answer the question!\n\nInput Format:\nEach test consists of several test cases. The first line contains an integer $t$ ($1 \\le t \\le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases.\n\nThe first line of each test case contains two integers $x_{1}, y_{1}$ ($0 \\le x_{1}, y_{1} \\le 10^{9}$, $x_{1} \\neq y_{1}$) β€” the score before Dima was distracted.\n\nThe second line of each test case contains two integers $x_{2}, y_{2}$ ($x_{1} \\le x_{2} \\le 10^{9}$, $y_{1} \\le y_{2} \\le 10^{9}$, $x_{2} \\neq y_{2}$) β€” the score when Dima returned.\n\nOutput Format:\nFor each test case, output \"YES\" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output \"NO\" without quotes.\n\nYou can output each letter in any case (for example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be recognized as a positive answer).\n\nTags:\n['greedy', 'implementation', 'math', 'sortings']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "6\n1 0\n5 0\n1 2\n3 2\n1 2\n4 5\n1 2\n4 3\n1 2\n1 2\n998244353 0\n1000000000 999999999", "metadata": { "func_name": null }, "output": "YES\nNO\nYES\nNO\nYES\nYES", "testtype": "stdin" } ], "style": "rule" }
{ "index": 74, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nRecently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture:\n\nThere is a variable $x$ and a constant $y$. The following operation is performed $k$ times:\n\n * increase $x$ by $1$, then * while the number $x$ is divisible by $y$, divide it by $y$. \n\nNote that both of these actions are performed sequentially within one operation.\n\nFor example, if the number $x = 16$, $y = 3$, and $k = 2$, then after one operation $x$ becomes $17$, and after another operation $x$ becomes $2$, because after adding one, $x = 18$ is divisible by $3$ twice.\n\nGiven the initial values of $x$, $y$, and $k$, Maxim wants to know what is the final value of $x$.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains an integer $t$ ($1 \\le t \\le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases.\n\nThe only line of each test case contains three integers $x$, $y$, and $k$ ($1 \\le x, k \\le 10^{9}$, $2 \\le y \\le 10^{9}$) β€” the initial variable, constant and the number of operations.\n\nOutput Format:\nFor each test case, output a single integer β€” the number obtained after applying $k$ operations.\n\nTags:\n['brute force', 'implementation', 'math', 'number theory']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "13\n1 3 1\n2 3 1\n24 5 5\n16 3 2\n2 2 1\n1337 18 1\n1 2 144133\n12345678 3 10\n998244353 2 998244353\n998244353 123456789 998244352\n998244354 998241111 998244352\n998244355 2 9982443\n1000000000 1000000000 1000000000", "metadata": { "func_name": null }, "output": "2\n1\n1\n2\n3\n1338\n1\n16936\n1\n21180097\n6486\n1\n2", "testtype": "stdin" } ], "style": "rule" }
{ "index": 75, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nOn another boring day, Egor got bored and decided to do something. But since he has no friends, he came up with a game to play.\n\nEgor has a deck of $n$ cards, the $i$-th card from the top has a number $a_i$ written on it. Egor wants to play a certain number of rounds until the cards run out. In each round, he takes a non-zero number of cards from the top of the deck and finishes the round. If the sum of the numbers on the cards collected during the round is between $l$ and $r$, inclusive, the round is won; otherwise, it is lost.\n\nEgor knows by heart the order of the cards. Help Egor determine the maximum number of rounds he can win in such a game. Note that Egor is not required to win rounds consecutively.\n\nInput Format:\nEach test consists of several test cases. The first line contains an integer $t$ ($1 \\le t \\le 10^{4}$) β€” the number of test cases. This is followed by a description of the test cases.\n\nThe first line of each test case contains three integers $n$, $l$, and $r$ ($1 \\le n \\le 10^{5}$, $1 \\le l \\le r \\le 10^9$).\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$) β€” the numbers on the cards from top to bottom.\n\nIt is guaranteed that the sum of $n$ for all test cases does not exceed $2 \\cdot 10^{5}$.\n\nOutput Format:\nFor each test case, output a single number β€” the maximum number of rounds Egor can win.\n\nTags:\n['binary search', 'data structures', 'dp', 'greedy', 'two pointers']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "8\n5 3 10\n2 1 11 3 7\n10 1 5\n17 8 12 11 7 11 21 13 10 8\n3 4 5\n3 4 2\n8 12 25\n10 7 5 13 8 9 12 7\n2 3 3\n5 2\n9 7 9\n2 10 5 1 3 7 6 2 3\n1 8 10\n9\n5 5 6\n1 4 2 6 4", "metadata": { "func_name": null }, "output": "3\n0\n1\n4\n0\n3\n1\n2", "testtype": "stdin" } ], "style": "rule" }
{ "index": 76, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nNikita loves mountains and has finally decided to visit the Berlyand mountain range! The range was so beautiful that Nikita decided to capture it on a map. The map is a table of $n$ rows and $m$ columns, with each cell containing a non-negative integer representing the height of the mountain.\n\nHe also noticed that mountains come in two types:\n\n * With snowy caps. * Without snowy caps. \n\nNikita is a very pragmatic person. He wants the sum of the heights of the mountains with snowy caps to be equal to the sum of the heights of the mountains without them. He has arranged with the mayor of Berlyand, Polikarp Polikarpovich, to allow him to transform the landscape.\n\nNikita can perform transformations on submatrices of size $k \\times k$ as follows: he can add an integer constant $c$ to the heights of the mountains within this area, but the type of the mountain remains unchanged. Nikita can choose the constant $c$ independently for each transformation. Note that $c$ can be negative.\n\nBefore making the transformations, Nikita asks you to find out if it is possible to achieve equality of the sums, or if it is impossible. It doesn't matter at what cost, even if the mountains turn into canyons and have negative heights.\n\nIf only one type of mountain is represented on the map, then the sum of the heights of the other type of mountain is considered to be zero.\n\nInput Format:\nEach test consists of several test cases. The first line contains an integer $t$ ($1 \\le t \\le 10^{4}$) β€” the number of test cases. This is followed by a description of test cases.\n\nThe first line of each test case contains three integers $n, m, k$ ($1 \\le n, m \\le 500, 1 \\le k \\le min(n, m)$).\n\nThe next $n$ lines of each test case contain $m$ integers $a_{i j}$ ($0 \\le a_{i j} \\le 10^{9}$) β€” the initial heights of the mountains.\n\nThe next $n$ binary strings of length $m$ for each test case determine the type of mountain, '$0$' β€” with snowy caps, '$1$' β€” without them.\n\nIt is guaranteed that the sum of $n \\cdot m$ for all test cases does not exceed $250\\,000$.\n\nOutput Format:\nFor each test case, output \"YES\" without quotes if it is possible to equalize the sums of the mountain heights, otherwise output \"NO\" without quotes. You can output each letter in any case (for example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be recognized as a positive answer).\n\nTags:\n['brute force', 'data structures', 'implementation', 'math', 'number theory']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "8\n3 3 2\n7 11 3\n4 2 3\n0 1 15\n100\n010\n000\n4 4 3\n123 413 24 233\n123 42 0 216\n22 1 1 53\n427 763 22 6\n0101\n1111\n1010\n0101\n3 3 2\n2 1 1\n1 1 2\n1 5 4\n010\n101\n010\n3 3 2\n2 1 1\n1 1 2\n1 5 3\n010\n101\n010\n3 4 3\n46 49 50 1\n19 30 23 12\n30 25 1 46\n1000\n0100\n0010\n5 4 4\n39 30 0 17\n22 42 30 13\n10 44 46 35\n12 19 9 39\n21 0 45 40\n1000\n1111\n0011\n0111\n1100\n2 2 2\n3 4\n6 7\n00\n00\n2 2 2\n0 0\n2 0\n01\n00", "metadata": { "func_name": null }, "output": "YES\nNO\nYES\nNO\nYES\nNO\nYES\nYES", "testtype": "stdin" } ], "style": "rule" }
{ "index": 77, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nLet $bit(x)$ denote the number of ones in the binary representation of a non-negative integer $x$.\n\nA subarray of an array is called $k$-good if it consists only of numbers with no more than $k$ ones in their binary representation, i.e., a subarray $(l, r)$ of array $a$ is good if for any $i$ such that $l \\le i \\le r$ condition $bit(a_{i}) \\le k$ is satisfied.\n\nYou are given an array $a$ of length $n$, consisting of consecutive non-negative integers starting from $0$, i.e., $a_{i} = i$ for $0 \\le i \\le n - 1$ (in $0$-based indexing). You need to count the number of $k$-good subarrays in this array.\n\nAs the answer can be very large, output it modulo $10^{9} + 7$.\n\nInput Format:\nEach test consists of multiple test cases. The first line contains an integer $t$ ($1 \\le t \\le 10^{4}$) β€” the number of test cases. The following lines describe the test cases.\n\nThe single line of each test case contains two integers $n$, $k$ ($1 \\le n \\le 10^{18}, 1 \\le k \\le 60$).\n\nOutput Format:\nFor each test case, output a single integer β€” the number of $k$-good subarrays modulo $10^{9} + 7$.\n\nTags:\n['bitmasks', 'brute force', 'combinatorics', 'divide and conquer', 'dp', 'math', 'meet-in-the-middle']\n\nTime Limit: 2000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "10\n6 1\n16 2\n1 1\n3 1\n31 3\n14 1\n1337 5\n100000 20\n795569939321040850 56\n576460752303423268 59", "metadata": { "func_name": null }, "output": "7\n35\n1\n6\n155\n8\n7323\n49965\n741136395\n66679884", "testtype": "stdin" } ], "style": "rule" }
{ "index": 78, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nYou have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following:\n\nWhat is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $a$ to be completely sorted in non-decreasing order?\n\nMore formally, you want to select a subarray of the array $(l, r)$ with the minimum value of $r - l + 1$. After that, you will sort the elements $a_{l}, a_{l + 1}, \\ldots, a_{r}$ and want the condition $a_{i} \\le a_{i + 1}$ to hold for all $1 \\le i < n$. If the array is already sorted in non-decreasing order, then $l$ and $r$ should be considered as equal to $-1$.\n\nNote that finding such $(l, r)$ does not change the array in any way. The modifications themselves take the form: assign $a_{pos} = x$ for given $pos$ and $x$.\n\nInput Format:\nEach test consists of several test cases. The first line contains an integer $t$ ($1 \\le t \\le 10$) β€” the number of test cases. Then follows the description of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 5 \\cdot 10^{5}$).\n\nThe second line of each test case contains $n$ integers $a_{i}$ ($0 \\le |a_{i}| \\le 10^{9}$) β€” the initial elements of the array $a$.\n\nThe third line of each test case contains a number $q$ ($0 \\le q \\le 5 \\cdot 10^{5}$) β€” the number of modifications to the array.\n\nThe following $q$ lines of each test case contain two integers $pos_{i}$ ($1 \\le pos_{i} \\le n$) and $val_{i}$ ($0 \\le |val_{i}| \\le 10^{9}$) β€” this means that for the $i$-th modification, $a_{pos_{i}}$ is assigned the value $val_{i}$.\n\nIt is guaranteed that the sum of $n$ and the sum of $q$ for all test cases does not exceed $5 \\cdot 10^{5}$.\n\nOutput Format:\nFor each test case, output $q + 1$ lines. Each line should contain $2$ integers $l, r$ β€” the boundaries of the minimum subarray, such that sorting it will make the array $a$ completely sorted. If $a$ is already sorted, then output $l = -1$, $r = -1$.\n\nTags:\n['binary search', 'data structures', 'sortings']\n\nTime Limit: 2500 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "2\n5\n2 2 3 4 5\n3\n2 1\n4 1\n1 1\n5\n1 2 3 4 5\n9\n1 4\n2 3\n5 2\n3 1\n1 1\n5 1\n4 1\n3 1\n2 1", "metadata": { "func_name": null }, "output": "-1 -1\n1 2\n1 4\n3 4\n-1 -1\n1 3\n1 3\n1 5\n1 5\n2 5\n2 5\n2 5\n2 5\n-1 -1", "testtype": "stdin" } ], "style": "rule" }
{ "index": 79, "solution": "", "split": "test" }
codeforces
[ { "content": "You are a helpful assistant.", "role": "system" }, { "content": "Solve the following coding problem using the programming language python:\n\n\nAn array of integers $a_1,a_2,\\cdots,a_n$ is beautiful subject to an integer $k$ if it satisfies the following:\n\n * The sum of $a_{j}$ over all $j$ such that $j$ is a multiple of $k$ and $1 \\le j \\le n $, itself, is a multiple of $k$. * More formally, if $\\sum_{k | j} a_{j}$ is divisible by $k$ for all $1 \\le j \\le n$ then the array $a$ is beautiful subject to $k$. Here, the notation ${k|j}$ means $k$ divides $j$, that is, $j$ is a multiple of $k$. \n\nGiven $n$, find an array of positive nonzero integers, with each element less than or equal to $10^5$ that is beautiful subject to all $1 \\le k \\le n$.\n\nIt can be shown that an answer always exists.\n\nInput Format:\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 100$). The description of the test cases follows.\n\nThe first and only line of each test case contains a single integer $n$ ($1 \\le n \\le 100$) β€” the size of the array.\n\nOutput Format:\nFor each test case, print the required array as described in the problem statement.\n\nTags:\n['constructive algorithms', 'math']\n\nTime Limit: 1000 ms\nMemory Limit: 256 MB\n\n\nThe input will be given via stdin and the output should be printed to stdout by your code.\n\nNow solve the problem by providing the code.", "role": "user" } ]
code
{ "ground_truth": [ { "input": "3\n3\n6\n7", "metadata": { "func_name": null }, "output": "4 22 18\n10 6 15 32 125 54\n23 18 27 36 5 66 7", "testtype": "stdin" } ], "style": "rule" }
{ "index": 80, "solution": "", "split": "test" }
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
2