id
stringlengths
12
12
text
stringlengths
52
14k
token_count
int64
10
5.52k
DIW3OSPSTXPE
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. Example 1: Input: "3+2*2" Output: 7 Example 2: Input: " 3/2 " Output: 1 Example 3: Input: " 3+5 / 2 " Output: 5 Note: You may assume that the given expression is always valid. Do not use the eval built-in library function. [
120
3E3NK269NSDO
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. Example 1: Input: "aacecaaa" Output: "aaacecaaa" Example 2: Input: "abcd" Output: "dcbabcd" [
73
2FBP90SKLCI7
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1. Some nodes initial are initially infected by malware.  Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware.  This spread of malware will continue until no more nodes can be infected in this manner. Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops. We will remove one node from the initial list.  Return the node that if removed, would minimize M(initial).  If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index. Note that if a node was removed from the initial list of infected nodes, it may still be infected later as a result of the malware spread.   Example 1: Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] Output: 0 Example 2: Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2] Output: 0 Example 3: Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2] Output: 1   Note: 1 < graph.length = graph[0].length <= 300 0 <= graph[i][j] == graph[j][i] <= 1 graph[i][i] == 1 1 <= initial.length <= graph.length 0 <= initial[i] < graph.length [
367
D8DF0L3XQ2ML
Given a sentence text (A sentence is a string of space-separated words) in the following format: First letter is in upper case. Each word in text are separated by a single space. Your task is to rearrange the words in text such that all words are rearranged in an increasing order of their lengths. If two words have the same length, arrange them in their original order. Return the new text following the format shown above.   Example 1: Input: text = "Leetcode is cool" Output: "Is cool leetcode" Explanation: There are 3 words, "Leetcode" of length 8, "is" of length 2 and "cool" of length 4. Output is ordered by length and the new first word starts with capital letter. Example 2: Input: text = "Keep calm and code on" Output: "On and keep calm code" Explanation: Output is ordered as follows: "On" 2 letters. "and" 3 letters. "keep" 4 letters in case of tie order by position in original text. "calm" 4 letters. "code" 4 letters. Example 3: Input: text = "To be or not to be" Output: "To be or to be not"   Constraints: text begins with a capital letter and then contains lowercase letters and single space between words. 1 <= text.length <= 10^5 [
297
1QD9P4CA9AQK
You are given a string s that consists of lower case English letters and brackets.  Reverse the strings in each pair of matching parentheses, starting from the innermost one. Your result should not contain any brackets.   Example 1: Input: s = "(abcd)" Output: "dcba" Example 2: Input: s = "(u(love)i)" Output: "iloveu" Explanation: The substring "love" is reversed first, then the whole string is reversed. Example 3: Input: s = "(ed(et(oc))el)" Output: "leetcode" Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string. Example 4: Input: s = "a(bcdefghijkl(mno)p)q" Output: "apmnolkjihgfedcbq"   Constraints: 0 <= s.length <= 2000 s only contains lower case English characters and parentheses. It's guaranteed that all parentheses are balanced. [
213
RQ2OT6XTAY7T
Given a string s of '(' , ')' and lowercase English characters.  Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if: It is the empty string, contains only lowercase characters, or It can be written as AB (A concatenated with B), where A and B are valid strings, or It can be written as (A), where A is a valid string.   Example 1: Input: s = "lee(t(c)o)de)" Output: "lee(t(c)o)de" Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted. Example 2: Input: s = "a)b(c)d" Output: "ab(c)d" Example 3: Input: s = "))((" Output: "" Explanation: An empty string is also valid. Example 4: Input: s = "(a(b(c)d)" Output: "a(b(c)d)"   Constraints: 1 <= s.length <= 10^5 s[i] is one of  '(' , ')' and lowercase English letters. [
283
QORJ9RYWWE86
Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. If no valid conversion could be performed, a zero value is returned. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign.   Then take as many numerical digits as possible, which gets 42. Example 3: Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4: Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical   digit or a +/- sign. Therefore no valid conversion could be performed. Example 5: Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.   Thefore INT_MIN (−231) is returned. [
438
UMRT2R6XSLHR
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,10,11,11] Output: 10 Note: Your solution should run in O(log n) time and O(1) space. [
107
U9D284TB67NH
There is an infinitely long street that runs west to east, which we consider as a number line. There are N roadworks scheduled on this street. The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5. Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point. Find the distance each of the Q people will walk. -----Constraints----- - All values in input are integers. - 1 \leq N, Q \leq 2 \times 10^5 - 0 \leq S_i < T_i \leq 10^9 - 1 \leq X_i \leq 10^9 - 0 \leq D_1 < D_2 < ... < D_Q \leq 10^9 - If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap. -----Input----- Input is given from Standard Input in the following format: N Q S_1 T_1 X_1 : S_N T_N X_N D_1 : D_Q -----Output----- Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever. -----Sample Input----- 4 6 1 3 2 7 13 10 18 20 13 3 4 2 0 1 2 3 5 8 -----Sample Output----- 2 2 10 -1 13 -1 The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2. The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2. The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1. [
486
8A2Y9TQLD10C
Given is an undirected connected graph with N vertices numbered 1 to N, and M edges numbered 1 to M. The given graph may contain multi-edges but not self loops. Each edge has an integer label between 1 and N (inclusive). Edge i has a label c_i, and it connects Vertex u_i and v_i bidirectionally. Snuke will write an integer between 1 and N (inclusive) on each vertex (multiple vertices may have the same integer written on them) and then keep only the edges satisfying the condition below, removing the other edges. Condition: Let x and y be the integers written on the vertices that are the endpoints of the edge. Exactly one of x and y equals the label of the edge. We call a way of writing integers on the vertices good if (and only if) the graph is still connected after removing the edges not satisfying the condition above. Determine whether a good way of writing integers exists, and present one such way if it exists. -----Constraints----- - 2 \leq N \leq 10^5 - N-1 \leq M \leq 2 \times 10^5 - 1 \leq u_i,v_i,c_i \leq N - The given graph is connected and has no self-loops. -----Input----- Input is given from Standard Input in the following format: N M u_1 v_1 c_1 \vdots u_M v_M c_M -----Output----- If there is no good way of writing integers, print No. Otherwise, print N lines. The i-th line should contain the integer written on Vertex i. Any good way of writing integers will be accepted. -----Sample Input----- 3 4 1 2 1 2 3 2 3 1 3 1 3 1 -----Sample Output----- 1 2 1 - We write 1, 2, and 1 on Vertex 1, 2, and 3, respectively. - Edge 1 connects Vertex 1 and 2, and its label is 1. - Only the integer written on Vertex 1 equals the label, so this edge will not get removed. - Edge 2 connects Vertex 2 and 3, and its label is 2. - Only the integer written on Vertex 2 equals the label, so this edge will not be removed. - Edge 3 connects Vertex 1 and 3, and its label is 3. - Both integers written on the vertices differ from the label, so this edge will be removed. - Edge 4 connects Vertex 1 and 3, and its label is 1. - Both integers written on the vertices equal the label, so this edge will be removed. - After Edge 3 and 4 are removed, the graph will still be connected, so this is a good way of writing integers. [
607
69JG59VQI4YJ
You are given a string S of length N consisting of lowercase English letters. Process Q queries of the following two types: - Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.) - Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive). -----Constraints----- - N, Q, i_q, l_q, and r_q are integers. - S is a string consisting of lowercase English letters. - c_q is a lowercase English letter. - 1 \leq N \leq 500000 - 1 \leq Q \leq 20000 - |S| = N - 1 \leq i_q \leq N - 1 \leq l_q \leq r_q \leq N - There is at least one query of type 2 in each testcase. -----Input----- Input is given from Standard Input in the following format: N S Q Query_1 \vdots Query_Q Here, Query_i in the 4-th through (Q+3)-th lines is one of the following: 1 i_q c_q 2 l_q r_q -----Output----- For each query of type 2, print a line containing the answer. -----Sample Input----- 7 abcdbbd 6 2 3 6 1 5 z 2 1 1 1 4 a 1 7 d 2 1 7 -----Sample Output----- 3 1 5 In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3. In the second query, S is modified to abcdzbd. In the third query, a contains one kind of letter: a, so we print 1. In the fourth query, S is modified to abcazbd. In the fifth query, S does not change and is still abcazbd. In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5. [
470
TN1M7KNOZV1F
There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even. Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written. Recently, they learned the operation called xor (exclusive OR).What is xor? For n non-negative integers x_1, x_2, \ldots, x_n, their xor, x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is defined as follows: - When x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even. For example, 3~\textrm{xor}~5 = 6. They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf. We know that the xor calculated by Snuke Cat i, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat i is a_i. Using this information, restore the integer written on the scarf of each Snuke Cat. -----Constraints----- - All values in input are integers. - 2 \leq N \leq 200000 - N is even. - 0 \leq a_i \leq 10^9 - There exists a combination of integers on the scarfs that is consistent with the given information. -----Input----- Input is given from Standard Input in the following format: N a_1 a_2 \ldots a_N -----Output----- Print a line containing N integers separated with space. The i-th of the integers from the left should represent the integer written on the scarf of Snuke Cat i. If there are multiple possible solutions, you may print any of them. -----Sample Input----- 4 20 11 9 24 -----Sample Output----- 26 5 7 22 - 5~\textrm{xor}~7~\textrm{xor}~22 = 20 - 26~\textrm{xor}~7~\textrm{xor}~22 = 11 - 26~\textrm{xor}~5~\textrm{xor}~22 = 9 - 26~\textrm{xor}~5~\textrm{xor}~7 = 24 Thus, this output is consistent with the given information. [
635
339I1UH0WZ1O
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively. Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors. Answer the following Q queries: - Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) -----Constraints----- - 2 \leq N \leq 10^5 - 1 \leq Q \leq 10^5 - 1 \leq a_i, b_i \leq N - 1 \leq c_i \leq N-1 - 1 \leq d_i \leq 10^4 - 1 \leq x_j \leq N-1 - 1 \leq y_j \leq 10^4 - 1 \leq u_j < v_j \leq N - The given graph is a tree. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N Q a_1 b_1 c_1 d_1 : a_{N-1} b_{N-1} c_{N-1} d_{N-1} x_1 y_1 u_1 v_1 : x_Q y_Q u_Q v_Q -----Output----- Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j. -----Sample Input----- 5 3 1 2 1 10 1 3 2 20 2 4 4 30 5 2 1 40 1 100 1 4 1 100 1 5 3 1000 3 4 -----Sample Output----- 130 200 60 The graph in this input is as follows: Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line. - Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130. - Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200. - Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths. [
681
6VGUBVC4J2BV
We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i. Vertex i has an integer a_i written on it. For every integer k from 1 through N, solve the following problem: - We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence. Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}. -----Constraints----- - 2 \leq N \leq 2 \times 10^5 - 1 \leq a_i \leq 10^9 - 1 \leq u_i , v_i \leq N - u_i \neq v_i - The given graph is a tree. - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N a_1 a_2 ... a_N u_1 v_1 u_2 v_2 : u_{N-1} v_{N-1} -----Output----- Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k. -----Sample Input----- 10 1 2 5 3 4 6 7 3 2 4 1 2 2 3 3 4 4 5 3 6 6 7 1 8 8 9 9 10 -----Sample Output----- 1 2 3 3 4 4 5 2 2 3 For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4. [
501
OZQ8DZ1SOANF
Write a program to take two numbers as input and print their difference if the first number is greater than the second number otherwise$otherwise$ print their sum. -----Input:----- - First line will contain the first number (N1$N1$) - Second line will contain the second number (N2$N2$) -----Output:----- Output a single line containing the difference of 2 numbers (N1−N2)$(N1 - N2)$ if the first number is greater than the second number otherwise output their sum (N1+N2)$(N1 + N2)$. -----Constraints----- - −1000≤N1≤1000$-1000 \leq N1 \leq 1000$ - −1000≤N2≤1000$-1000 \leq N2 \leq 1000$ -----Sample Input:----- 82 28 -----Sample Output:----- 54 [
199
MVTQ1LJANVKG
Witua is a little student from the University of Lviv. He enjoys studying math. Witua knows a lot of famous mathematicians like Eratosthenes, Pythagoras, Fermat, Diophantus, Furko, Gauss and so on. However, his favorite one is Euler. The only thing Witua likes more than Euler is Euler’s totient function φ. He is exploring the nature of this function. One of the steps of his work is finding φ(i)/i for all 2≤i≤N. He doesn’t need to know every such value, but Witua wonders for what value i, is φ(i)/i the maximum he can get? Help little student to find such i that φ(i)/i is maximum among all the 2≤i≤N. -----Input----- The first line contains single integer T - the number of test cases. Each of the next T lines contains a single integer N. -----Output----- For every test case output i such that φ(i)/i is maximum among all i (2≤i≤N) in a separate line. -----Constrains----- T (1≤T≤500 ) N(2≤N≤10^18) -----Example----- Input: 3 2 3 4 Output: 2 3 3 Explanationφ(2)/2=1/2 φ(3)/3=2/3 φ(4)/4=2/4 [
313
PV8KWR805TN1
Almir had a small sequence $A_1, A_2, \ldots, A_N$. He decided to make $K$ copies of this sequence and concatenate them, forming a sequence $X_1, X_2, \ldots, X_{NK}$; for each valid $i$ and $j$ ($0 \le j < K$), $X_{j \cdot N + i} = A_i$. For example, if $A = (1, 2, 3)$ and $K = 4$, the final sequence is $X = (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)$. A pair $(i, j)$, where $1 \le i < j \le N$, is an inversion if $X_i > X_j$. Find the number of inversions in the final sequence $X$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $K$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the number of inversions in the sequence $X$. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 100$ - $1 \le K \le 10^6$ - $1 \le A_i \le 10^9$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 3 2 1 3 4 100 99 2 1000 24 -----Example Output----- 12 30000 [
429
LH6K9DTK8YV7
Indian National Olympiad in Informatics 2015 A string is any nonempty sequence of 0s and 1s. Examples of strings are 00, 101, 111000, 1, 0, 01. The length of a string is the number of symbols in it. For example, the length of 111000 is 6. If u and v are strings, then uv is the string obtained by concatenating u and v. For example if u = 110 and v = 0010 then uv = 1100010. A string w is periodic if there exists a string v such that w = vn = vv · · · v (n times), for some n ≥ 2. Note that in this case the length of v is strictly less than that of w. For example, 110110 is periodic, because it is vv for v = 110. Given a positive integer N , find the number of strings of length N which are not periodic. Report the answer modulo M . The non-periodic strings of length 2 are 10 and 01. The non- periodic strings of length 3 are 001, 010, 011, 100, 101, and 110. -----Input format----- A single line, with two space-separated integers, N and M . -----Output format----- A single integer, the number of non-periodic strings of length N , modulo M . -----Test Data----- In all subtasks, 2 ≤ M ≤ 108. The testdata is grouped into 4 subtasks. Subtask 1 (10 marks) 1 ≤ N ≤ 4000. N is the product of two distinct prime numbers. Subtask 2 (20 marks) 1 ≤ N ≤ 4000. N is a power of a prime number. Subtask 3 (35 marks) 1 ≤ N ≤ 4000. Subtask 4 (35 marks) 1 ≤ N ≤ 150000. -----Example----- Here is the sample input and output corresponding to the example above: -----Sample input----- 3 176 -----Sample output----- 6 Note: Your program should not print anything other than what is specified in the output format. Please remove all diagnostic print statements before making your final submission. A program with extraneous output will be treated as incorrect! [
484
U2OWLEVL1IKX
Finally, the pandemic is over in ChefLand, and the chef is visiting the school again. Chef likes to climb the stairs of his school's floor by skipping one step, sometimes chef climbs the stairs one by one. Simply, the chef can take one or 2 steps in one upward movement. There are N stairs between ground and next floor. The chef is on the ground floor and he wants to go to the next floor with Cheffina but, Cheffina asks chef in how many ways, the chef can reach the next floor normally or any combination of skipping one step, where order doesn't matter. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, two integers $N$. -----Output:----- For each test case, output in a single line answer as the number of ways. -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq N \leq 10^5$ -----Sample Input:----- 1 3 -----Sample Output:----- 2 -----EXPLANATION:----- ways: [1,1,1], here chef climb to the next floor, one by one stair. [1,2], here chef climb to the next floor, one step first and after that 2 stairs at once. Note, [2,1] consider the same as that of [1,2] hence ignored. [
311
DUO21KOEM8I8
Indian National Olympiad in Informatics 2016 There are k types of brackets each with its own opening bracket and closing bracket. We assume that the first pair is denoted by the numbers 1 and k+1, the second by 2 and k+2 and so on. Thus the opening brackets are denoted by 1,2,.., k, and the corresponding closing brackets are denoted by k+1,k+2,..., 2*k respectively. Some sequences with elements from 1,2, ... 2*k form well-bracketed sequences while others don't. A sequence is well-bracketed, if we can match or pair up opening brackets and closing brackets of the same type in such a way that the following holds: 1) every bracket is paired up 2) in each matched pair, the opening bracket occurs before the closing bracket 3) for a matched pair, any other matched pair lies either completely between them or outside them. For the examples discussed below, let us assume that k = 2. The sequence 1,1,3 is not well-bracketed as one of the two 1's cannot be paired. The sequence 3,1,3,1 is not well-bracketed as there is no way to match the second 1 to a closing bracket occurring after it. The sequence 1,2,3,4 is not well-bracketed as the matched pair 2,4 is neither completely between the matched pair 1,3 nor completely outside it. That is, the matched pairs cannot overlap. The sequence 1,2,4,3,1,3 is well-bracketed. We match the first 1 with the first 3, the 2 with the 4 and the second 1 with the second 3, satisfying all the 3 conditions. If you rewrite these sequences using [,{,],} instead of 1,2,3,4 respectively, this will be quite clear. In this problem you are given a sequence of brackets, of length N: B[1], .., B[N], where each B[i] is one of the brackets. You are also given an array of Values: V[1],.., V[N]. Among all the subsequences in the Values array, such that the corresponding bracket subsequence in the B Array is a well-bracketed sequence, you need to find the maximum sum. Suppose N = 6, k = 3 and the values of V and B are as follows: i 1 2 3 4 5 6 V[i] 4 5 -2 1 1 6 B[i] 1 3 4 2 5 6 Then, the brackets in positions 1,3 form a well-bracketed sequence (1,4) and the sum of the values in these positions is 2 (4 + -2 = 2). The brackets in positions 1,3,4,5 form a well-bracketed sequence (1,4,2,5) and the sum of the values in these positions is 4. Finally, the brackets in positions 2,4,5,6 forms a well-bracketed sequence (3,2,5,6) and the sum of the values in these positions is 13. The sum of the values in positions 1,2,5,6 is 16 but the brackets in these positions (1,3,5,6) do not form a well-bracketed sequence. You can check the best sum from positions whose brackets form a well-bracketed sequence is 13. -----Input format----- One line, which contains (2*N + 2) space separate integers. The first integer denotes N. The next integer is k. The next N integers are V[1],..., V[N]. The last N integers are B[1],.., B[N]. -----Output format----- One integer, which is the maximum sum possible satisfying the requirement mentioned above. -----Test data----- 1 ≤ k ≤ 7 -106 ≤ V[i] ≤ 106, for all i 1 ≤ B[i] ≤ 2*k, for all i. Subtask 1 (40 Marks) 1 ≤ n ≤ 10. Subtask 2 (60 Marks) 1 ≤ n ≤ 700. -----Sample Input----- 6 3 4 5 -2 1 1 6 1 3 4 2 5 6 -----Sample Output----- 13 [
969
RA6V40FL46FP
Write a program that takes in a letterclass ID of a ship and display the equivalent string class description of the given ID. Use the table below. Class ID Ship ClassB or bBattleShipC or cCruiserD or dDestroyerF or fFrigate -----Input----- The first line contains an integer T, the total number of testcases. Then T lines follow, each line contains a character. -----Output----- For each test case, display the Ship Class depending on ID, in a new line. -----Constraints----- - 1 ≤ T ≤ 1000 -----Example----- Input 3 B c D Output BattleShip Cruiser Destroyer [
146
G9256BIJW11V
Nature photographing may be fun for tourists, but it is one of the most complicated things for photographers. To capture all the facets of a bird, you might need more than one cameras. You recently encountered such a situation. There are $n$ photographers, so there are $n$ cameras in a line on the x-axis. All the cameras are at distinct coordinates. You want to pair up these cameras ($n$ is even) in such a way that the sum of angles subtended on the bird by the pair of cameras is maximized. Formally, let A, B be two cameras, and let P be the bird to be captured by these two cameras. The angle will APB. Note: All angles are in radians. -----Input----- - The first line of the input contains an integer $T$ denoting the number of test cases. The description of the test cases follows. - The first line of each test case contains an integer $n$. - The second line of each test case contains $n$ space-separated integers denoting the $x_i$ coordinates of the cameras. - The third line of each test case contains two space-separated integers $P, Q$ denoting the x and y coordinates of the bird respectively. -----Output----- For each test case, output your answer in a single line. Your answer would be considered correct if its absolute error is less than or equal to 1e-6 of the actual answer. -----Constraints----- - $1 \le T \le 10$ - $2 \le n \leq 100$ - $1 \le x_i \leq 300$ - $0 \le P \leq 300$ - $1 \le Q \leq 300$ -----Example Input----- 2 2 0 1 0 1 2 0 1 100 1 -----Example Output----- 0.785398163397 0.000100999899 -----Explanation----- Note: $1 \leq x_i$ is not being satisfied by the sample input, but will be satisfied in the actual test data. Testcase 1: There are only 2 cameras, so they have to paired up with each other. And the angle subtended by then is 45 degrees. Converting this to radians gives the output. [
483
KFG1R97ABLII
Three Best Friends $AMAN$ , $AKBAR$ , $ANTHONY$ are planning to go to “GOA” , but just like every other goa trip plan there is a problem to their plan too. Their parents will only give permission if they can solve this problem for them They are a given a number N and they have to calculate the total number of triplets (x ,y ,z) Satisfying the given condition y * x +z=n. For ex if N=3 Then there exist 3 triplets( x ,y ,z): (1,1,2) , (1,2,1) , (2,1,1) which satisfy the condition Help the group to get permission for the trip -----Input:----- - First line will contain the number $N$. -----Output:----- the possible number of triplets satisfying the given condition -----Constraints----- - $2 \leq N \leq 10^6$ -----Sample Input:----- 3 -----Sample Output:----- 3 -----EXPLANATION:----- there exist 3 triplets ( x ,y ,z): (1,1,2) , (1,2,1) , (2,1,1) which satisfy the condition [
273
K1IS23U5K6R4
Maheshmati and Sangu are playing a game. First, Maheshmati gives Sangu a sequence of $N$ distinct integers $a_1, a_2, \dots, a_N$ (not necessarily sorted) and an integer $K$. Sangu has to create all subsequences of this sequence with length $K$. For each subsequence, he has to write down the product of $K-2$ integers: all elements of this subsequence except the minimum and maximum element. Sangu wins the game if he is able to write down all these numbers and tell Maheshmati their product (modulo $10^9+7$, since it can be very large). However, Sangu is a very lazy child and thus wants you to help him win this game. Compute the number Sangu should tell Maheshmati! -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $K$. - The second line contains $N$ space-separated integers $a_1, a_2, \dots, a_N$. -----Output----- For each test case, print a single line containing one integer — the product of all numbers written down by Sangu modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10$ - $3 \le N \le 5,000$ - $3 \le K \le N$ - $1 \le a_i \le 10,000$ for each valid $i$ - the numbers $a_1, a_2, \dots, a_N$ are pairwise distinct -----Subtasks----- Subtask #1 (20 points): $1 \le N \le 10$ Subtask #2 (80 points): original constraints -----Example Input----- 1 4 3 1 2 3 4 -----Example Output----- 36 -----Explanation----- Example case 1: There are four possible subsequences: - $[1, 2, 3]$ (Sangu should write down $2$.) - $[1, 3, 4]$ (Sangu should write down $3$.) - $[1, 2, 4]$ (Sangu should write down $2$.) - $[2, 3, 4]$ (Sangu should write down $3$.) The required product is $2 \cdot 3 \cdot 2 \cdot 3 = 36$. [
551
WH99JTLNNDE4
There are total $N$ cars in a sequence with $ith$ car being assigned with an alphabet equivalent to the $ith$ alphabet of string $S$ . Chef has been assigned a task to calculate the total number of cars with alphabet having a unique even value in the given range X to Y (both inclusive) . The value of an alphabet is simply its position in alphabetical order e.g.: a=1, b=2, c=3… The chef will be given $Q$ such tasks having varying values of $X$ and $Y$ Note: string $S$ contains only lowercase alphabets -----Input----- First line of input contains a string $S$ of length $N$. Second line contains an integer denoting no. of queries $Q$. Next q lines contain two integers denoting values of $X$ and $Y$. -----Output----- For each query print a single integer denoting total number of cars with alphabet having a unique even value in the given range $X$ to $Y$. -----Constraints----- - $1 \leq n \leq 10^5$ - $1 \leq q \leq 10^5$ -----Example Input----- bbccdd 5 1 2 3 4 5 6 1 6 2 5 -----Example Output----- 1 0 1 2 2 -----Explanation:----- Example case 1: Query 1: range 1 to 2 contains the substring $"bb"$ where each character has a value of 2. Since we will only be considering unique even values, the output will be 1 [
343
O3RW5WJZCYER
You are given three numbers $a$, $b$, $c$ . Write a program to determine the largest number that is less than or equal to $c$ and leaves a remainder $b$ when divided by $a$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains a single line of input, having three integers $a$, $b$, $c$. -----Output:----- - For each testcase, output in a single line the largest number less than or equal to $c$. -----Constraints:----- - $1 \leq T \leq 100000$ - $0 \leq b < a < c \leq$ $10$^18 -----Sample Input:----- 1 7 2 10 -----Sample Output:----- 9 [
179
JEXARKXMJBCI
Aureole the Techno-cultural fest of JEC thought of conducting a workshop on big data, as the topic is hot everyone wants to take part but due to limited seats in the Jashan Auditorium there is a selection criteria, a problem is given. the problem states a string is to be compressed when two or more consecutive characters are same they are to be compressed into one character and the original number count is to be written after it ex. aaabb -> a3b2 where every character is of 8bits and every integer is of 32bits you are asked to find the difference in size between original string and compressed string -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input $S$ the string to be compressed. -----Output:----- For each testcase, output in a single line The difference in size. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq |S| \leq 10^5$ -----Subtasks----- - 40 points : S will contain only a-z - 60 points : S will contain 0-9 characters also -----Sample Input:----- 1 aaabb -----Sample Output:----- -40 -----EXPLANATION:----- the resulting string will be a3b2 its size will be 80, original string size will be 40 so ans= 40-80 [
313
7QEB04P2QV18
Given an Array of length $N$ containing elements $Ai$ ( i = 1 to n ) . You have to handle $Q$ queries on this array . Each Query is of two types k=(1 or 2). Type 1:- $k$ $l$ $r$ in which you have to tell whether the product of numbers in range l to r results in a perfect square or not. if product of numbers in range $l$ to$r$ is a perfect square then simply output YES else output NO. Type 2:- $k$ $i$ $val$ Multiply the value present at index $i$ with $val$. Note#1: 1 based indexing in each query. Note#2: Values of prime factors of all numbers $val$ and $Ai$ is between 2 to 100 only. -----Input:----- - First line will contain $N$, denoting the size of the array. Then the next line follow. - N integers $Ai - An$. - Third line will contain $Q$, denoting the number of queries. Then the next $Q$ lines follow -description of each query. - Each query consists of either type 1 or type 2 and each query gives you three elements either -{$k$ $l$ $r$} or {$k$ $i$ $val$} -----Output:----- For each Query of Type 1 Output either "YES" or "NO" Without Quotes. -----Constraints----- - $1 \leq N \leq 20000$ - $1 \leq Q \leq 20000$ - $2 \leq Ai \leq 1000000$ - $1 \leq i ,l,r \leq N$ - $1 \leq val \leq 1000000$ - $1 \leq l \leq r$ -----Subtasks----- Subtask 1 :-40 points - Values of prime factors of all numbers $val$ and $Ai$ is between 2 to 40 only. Subtask 2 :- 60 points - Original Constraints -----Sample Input:----- 4 2 2 3 4 4 1 1 2 1 3 4 2 3 3 1 1 4 -----Sample Output:----- YES NO YES -----EXPLANATION:----- -Query 1 :- product of numbers in range 1 to 2=2 * 2=4 (perfect square so YES) -Query 2:- product of numbers in range 3 to 4 = 3 * 4 = 12 (not perfect square so NO) -Query 3:- multiply number at index3 with 3 so number at index 3= 3*3 = 9 . -Query 4:- product of numbers in range 1 to 4 = 2 * 2 * 9 * 4 = 144 (perfect square so YES) [
631
6TVHAOLKIOUW
Recently in JEC ants have become huge, the Principal is on a journey to snipe them !! Principal has limited $N$ practice Bullets to practice so that he can be sure to kill ants. - The Practice ground has max length $L$. - There is a Limit X such that if the bullet is fired beyond this, it will destroy and it wont be of any further use. - Bullet can be reused if fired in a range strictly less than X. He wants to find minimum number of shots taken to find the distance X by using $N$ bullets. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, two integers $N, L$. -----Output:----- For each testcase, output in a single line answer the minimum number of shots to find the distance X. -----Constraints----- - $1 \leq T \leq 10$ - $2 \leq N,L \leq 100$ *N is always less than equal to square root of L -----Subtasks----- - 10 points : $ N = 1$ - 40 points : $ N = 2$ - 50 points : Original Constraints. -----Sample Input:----- 2 1 10 2 10 -----Sample Output:----- 10 4 -----EXPLANATION:----- - There is only single bullet which is to be fired from distance 1 to 10 to get the distance X so in the worst case it can take up to 10 shots to find the distance X. - there are 2 bullets and distance 10 meters suppose if distance X is 10 we can get to that by firing first bullet at 4 then 7 then 9 then at 10 it will break it took only 4 turns, and if the distance X was 3, we can get that by firing first bullet at 4 it will get destroyed than we use 2nd bullet at 1 , 2, 3 and 2nd bullet will also break it also took 4 turns. You can check for any position minimum number of turns will be at most 4. [
463
SH355CR3ACJZ
Given an integer N. Integers A and B are chosen randomly in the range [1..N]. Calculate the probability that the Greatest Common Divisor(GCD) of A and B equals to B. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case consists of a single integer N on a separate line. -----Output----- For each test case, output a single line containing probability as an irreducible fraction. -----Example----- Input: 3 1 2 3 Output: 1/1 3/4 5/9 -----Constraints----- 1<=T<=103 1<=N<=109 [
147
EU6FFGM07P6E
The median of a sequence is the element in the middle of the sequence after it is sorted. For a sequence with even size, the median is the average of the two middle elements of the sequence after sorting. For example, for a sequence $A = [1, 3, 3, 5, 4, 7, 11]$, the median is equal to $4$, and for $A = [2, 3, 4, 5]$, the median is equal to $(3+4)/2 = 3.5$. Fulu is a famous programmer in his country. He wrote the following program (given in pseudocode) for finding the median in a sequence $A$ with length $N$: read(N) read(A[0], A[1], ..., A[N-1]) sort(A) # simultaneously remove elements A[K] and A[N mod K] from the array A # the order of the remaining N-2 elements is unchanged remove(A[K], A[N mod K]) return A[(N-2)/2] # integer division The program takes an integer $K$ as a parameter. Fulu can choose this integer arbitrarily between $1$ and $N-1$ inclusive. Little Lima, Fulu's friend, thinks Fulu's program is wrong (as always). As finding one counterexample would be an easy task for Lima (he has already found the sequence $A = [34, 23, 35, 514]$, which is a counterexample for any $K \le 3$), Lima decided to make hacking more interesting. Fulu should give him four parameters $S, K, m, M$ (in addition to $N$) and Lima should find the lexicographically smallest proper sequence $A$ with length $N$ as a counterexample. We say that a sequence $A$ with length $N$ ($0$-indexed) is proper if it satisfies the following conditions: - it contains only positive integers - $A_0 + A_1 +A_2 + \dots + A_{N-1} = S$ - $m \le A_i \le M$ for each $0 \le i < N$ - the number returned by Fulu's program, ran with the given parameter $K$, is different from the correct median of the sequence $A$ Can you help Lima find the lexicographically smallest counterexample or determine that Fulu's program works perfectly for the given parameters? -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains five space-separated integers $N$, $S$, $K$, $m$ and $M$. -----Output----- For each test case, if there is no proper sequence, print a single line containing the integer $-1$; otherwise, print a single line containing $N$ space-separated integers — the lexicographically smallest proper sequence. -----Constraints----- - $1 \le T \le 8$ - $3 \le N \le 10^5$ - $1 \le K \le N-1$ - $1 \le S \le 10^9$ - $1 \le m \le M \le S$ -----Example Input----- 2 3 6 1 1 5 4 4 2 1 3 -----Example Output----- 1 1 4 -1 -----Explanation----- Example case 1: For the sequence $A = [1, 1, 4]$, it is already sorted, so the program would just remove the first two elements ($K=1$, $N\%K=0$) and return the only remaining element $4$. Obviously, the median of the original sequence is $1$. It can be shown that there is no lexicographically smaller proper sequence. Example case 2: The only possible sequence is $A = [1, 1, 1, 1]$, which is not proper, since Fulu's program will give the correct answer $1$ for it. [
879
3T9235NWXZKG
Shashank is playing a game with his friends. There are n sticks located in a row at points $a_1,a_2, ...,a_n$. Each stick has a height- $h_i$. A person can chop a stick down, after which it takes over one of the regions [$a_i$ - $h_i$, $a_i$] or [$a_i$, $a_i$ + $h_i$]. The stick that is not chopped remains at the point $a_i$. A person can chop a stick in a particular direction if the region to be taken up by the chopped stick does not overlap with an already existing point. The winner $($s$)$ of the game will be one or more people who can answer the question: What is the maximum number of sticks that can be chopped? Shashank wants to win the game and hence he needs needs your help in finding out what is the maximum number of sticks that can be chopped down. -----Input:----- - The first line of each input contains a single integer n. - n lines follow. Each of the n lines contain a pair of integers: $a_i,h_i$. -----Output:----- Output in a single line answer- the maximum number of sticks that can be chopped down. -----Constraints----- - $1 \leq n \leq 10^5$ - $1 \leq a_i,h_i \leq 10^9$ - The pairs are given in the order of ascending $a_i$. No two sticks are located at the same point. -----Sample Input 1:----- 5 1 2 2 1 5 10 10 9 19 1 -----Sample Input 2:----- 5 1 2 2 1 5 10 10 9 20 1 -----Sample Output 1:----- 3 -----Sample Output 2:----- 4 -----EXPLANATION:----- In the first example you can fell the sticks as follows: - chop the stick 1 to the left — now it will take over the region $[ - 1;1]$ - chop the stick 2 to the right — now it will take over the region $[2;3]$ - spare the stick 3— it will take over the point $5$ - spare the stick 4— it will take over the point $10$ - chop the stick 5 to the right — now it will take over the region $[19;20]$ [
531
1F93IHP7PEG3
To help Lavanya learn all about binary numbers and binary sequences, her father has bought her a collection of square tiles, each of which has either a 0 or a 1 written on it. Her brother Nikhil has played a rather nasty prank. He has glued together pairs of tiles with 0 written on them. Lavanya now has square tiles with 1 on them and rectangular tiles with two 0's on them, made up of two square tiles with 0 stuck together). Thus, she can no longer make all possible binary sequences using these tiles. To amuse herself, Lavanya has decided to pick a number $N$ and try and construct as many binary sequences of length $N$ as possible using her collection of tiles. For example if $N$ = 1, she can only make the sequence 1. For $N$=2, she can make 11 and 00. For $N$=4, there are 5 possibilities: 0011, 0000, 1001, 1100 and 1111. Lavanya would like you to write a program to compute the number of arrangements possible with $N$ tiles so that she can verify that she has generated all of them. Since she cannot count beyond 15746, it is sufficient to report this number modulo 15746. -----Input:----- A single line with a single integer $N$. -----Output:----- A single integer indicating the number of binary sequences of length $N$, modulo 15746, that Lavanya can make using her tiles. -----Constraints:----- You may assume that $N \leq$ 1000000. -----Sample Input:----- 4 -----Sample Output:----- 5 -----Explanation:----- This corresponds to the example discussed above. [
368
FKMTN35M3PP3
The chef is playing a game of long distance. Chef has a number K and he wants to find the longest distance between the index of the first and the last occurrence of K in a given array of N numbers. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains two lines of input. - Next line with Two integers in one line $K, N$. - Next line with $N$ space-separated integers. -----Output:----- For each test case, output in a single line answer as the index of first and last occurrence of K in the given array. Note: Here Indexing is from 1 not 0 based. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq k \leq 10^5$ - $1 \leq N \leq 10^5$ -----Sample Input:----- 2 2 6 2 3 4 2 1 6 4 6 2 3 4 2 1 6 -----Sample Output:----- 3 0 -----EXPLANATION:----- For 1) Index of First and last occurrence of 2 in the given array is at 1 and 4, i.e. distance is 3. For 2) 4 occurs only once in the given array hence print 0. [
302
EHKY2IUXMVZ6
Vasya's older brother, Petya, attends an algorithm course in his school. Today he learned about matchings in graphs. Formally, a set of edges in a graph is called a matching if no pair of distinct edges in the set shares a common endpoint. Petya instantly came up with an inverse concept, an antimatching. In an antimatching, any pair of distinct edges should have a common endpoint. Petya knows that finding a largest matching in a graph is a somewhat formidable task. He wonders if finding the largest antimatching is any easier. Help him find the number of edges in a largest antimatching in a given graph. -----Input:----- The first line contains T$T$, number of test cases per file. The first line of each test case contains two integers n$n$ and m−$m-$ the number of vertices and edges of the graph respectively (1≤n≤104$1 \leq n \leq 10^4$, 0≤m≤104$0 \leq m \leq 10^4$). The next m$m$ lines describe the edges. The i$i$-th of these lines contains two integers ui$u_i$ and vi−$v_i-$ the indices of endpoints of the i$i$-th edge (1≤ui,vi≤n$1 \leq u_i, v_i \leq n$, ui≠vi$u_i \neq v_i$). It is guaranteed that the graph does not contain self-loops nor multiple edges. It is not guaranteed that the graph is connected. -----Output:----- Print a single number per test case −$-$ the maximum size of an antichain in the graph. -----Constraints----- - 1≤T≤10$1 \leq T \leq 10$ - 1≤n≤104$1 \leq n \leq 10^4$ - 0≤m≤104$0 \leq m \leq 10^4$ - 1≤ui,vi≤n$1 \leq u_i, v_i \leq n$ - ui≠vi$u_i \neq v_i$ -----Sample Input:----- 3 3 3 1 2 1 3 2 3 4 2 1 2 3 4 5 0 -----Sample Output:----- 3 1 0 -----EXPLANATION:----- In the first sample all three edges form an antimatching. In the second sample at most one of the two edges can be included in an antimatching since they do not share common endpoints. In the third sample there are no edges, hence the answer is 0$0$. [
587
IPQQNNM43AEC
Chef got in the trouble! He is the king of Chefland and Chessland. There is one queen in Chefland and one queen in Chessland and they both want a relationship with him. Chef is standing before a difficult choice… Chessland may be considered a chessboard with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote a unit square in row $r$ and column $c$ by $(r, c)$. Chef lives at square $(X, Y)$ of this chessboard. Currently, both queens are living in Chessland too. Each queen, when alone on the chessboard, can see all squares that lie on the same row, column or diagonal as itself. A queen from $(x_q, y_q)$ cannot see a square $(r, c)$ if the square $(X, Y)$ is strictly between them. Of course, if the queens can see each other, the kingdom will soon be in chaos! Help Chef calculate the number of possible configurations of the queens such that the kingdom will not be in chaos. A configuration is an unordered pair of distinct squares $(x_{q1}, y_{q1})$ and $(x_{q2}, y_{q2})$ such that neither of them is the square $(X, Y)$. Two configurations are different if the position of queen $1$ is different or the position of queen $2$ is different. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains four space-separated integers $N$, $M$, $X$ and $Y$. -----Output----- For each test case, print a single line containing one integer — the number of configurations such that the kingdom will not be in chaos. -----Constraints----- - $1 \le T \le 1000$ - $1 \le X \le N \le 10^2$ - $1 \le Y \le M \le 10^2$ - $2 \le N, M$ -----Example Input----- 2 3 3 2 2 4 4 2 3 -----Example Output----- 24 94 -----Explanation----- Example case 1: Half of these configurations are: - $(1, 1), (3, 3)$ - $(1, 1), (2, 3)$ - $(1, 1), (3, 2)$ - $(1, 2), (3, 3)$ - $(1, 2), (3, 2)$ - $(1, 2), (3, 1)$ - $(1, 3), (3, 1)$ - $(1, 3), (3, 2)$ - $(1, 3), (2, 1)$ - $(2, 1), (2, 3)$ - $(2, 1), (1, 3)$ - $(2, 1), (3, 3)$ [
671
LANBO0ASASGQ
2021 was approaching and the world was about to end. So 2 gods Saurabhx and Saurabhy (from Celesta) created the Cyberverse. But this time disappointed with humans both the gods decided not to have humans in this world. So they created a world of cyborgs. A world without humans. Isn’t it interesting? So let us dive into the cyberverse and have a look at their problems. There are $N$ kid cyborgs with Chief Cyborg '100gods' and he has $K$ weapons with him. He wants to distribute those $K$ weapons among $N$ kid cyborgs. Since all the kid cyborgs are very good friends, so they set a rule among themselves for taking those weapons. The rule states that the difference between kid cyborg having the maximum weapons and the kid cyborg having minimum weapons should be less than or equal to $1$. Find the value of the minimum number of weapons a kid cyborg can have when all the $K$ weapons are distributed among them. -----Input:----- - The first line contains an integer $T$, denoting the number of test cases. - Each of the next $T$ lines will contain two space-separated integers denoting $N$ and $K$ respectively. -----Output:----- - For each test case ,output a single line containing an integer $X$ denoting the minimum number of weapons a kid cyborg can have in that test case. -----Constraints:----- - $1 \leq T \leq 10^5$ - $1 \leq N \leq 10^5$ - $1 \leq K \leq 10^9$ -----Sample Input:----- 1 5 8 -----Expected Output:----- 1 -----Explanation----- - There are $5$ kids and $8$ weapons. - Hence we will distribute the weapons such that $3$ kids have $2$ weapons each and the remaining $2$ kids have $1$ weapon each. - Hence the minimum number of weapons a kid cyborg has is $1$. ( That is, $min(1,2)$ = $1$ ) [
458
JCUEI7Z05W5L
Zonal Computing Olympiad 2015, 29 Nov 2014 We say that two integers x and y have a variation of at least K, if |x − y| ≥ K (the absolute value of their difference is at least K). Given a sequence of N integers a1,a2,...,aN and K, the total variation count is the number of pairs of elements in the sequence with variation at least K, i.e. it is the size of the set of pairs {(i,j)|1≤i<j≤N and|ai−aj|≥K} For example if K = 1 and the sequence is 3,2,4 the answer is 3. If K = 1 and the sequence is 3, 1, 3 then the answer is 2. Your task is to write a program that takes a sequence and the value K as input and computes the total variation count. -----Input format----- The first line contains two positive integers N and K, separated by a space. This is followed by a line containing N integers separated by space giving the values of the sequence. -----Output format----- A single integer in a single line giving the total variation count. -----Test data----- You may assume that all integers in the input are in the range 0 to 10^8 inclusive. Subtask 1 (40 marks) : 1 ≤ N ≤ 4000, 1 ≤ K ≤ 10^8 Subtask 2 (60 marks) : 1 ≤ N ≤ 65000, 1 ≤ K ≤ 10^8 -----Sample Input----- 3 1 3 1 3 -----Sample Output----- 2 [
354
F2SY4CFK7DBE
Sebi goes to school daily with his father. They cross a big highway in the car to reach to the school. Sebi sits in front seat beside his father at driving seat. To kill boredom, they play a game of guessing speed of other cars on the highway. Sebi makes a guess of other car's speed being SG kph, his father FG kph. The highway is usually empty, so the drivers use cruise control, i.e. vehicles run at a constant speed. There are markers on the highway at a gap of 50 meters. Both father-son duo wants to check the accuracy of their guesses. For that, they start a timer at the instant at which their car and the other car (which speed they are guessing) are parallel to each other (they need not to be against some marker, they can be in between the markers too). After some T seconds, they observe that both the cars are next to some markers and the number of markers in between the markers of their car and the other car is D - 1 (excluding the markers next to both the cars). Also, they can observe these markers easily because the other car is faster than their. Speed of Sebi's father's car is S. Using this information, one can find the speed of the other car accurately. An example situation when Sebi's father starts the timer. Notice that both the car's are parallel to each other. Example situation after T seconds. The cars are next to the markers. Here the value of D is 1. The green car is Sebi's and the other car is of blue color. Sebi's a child, he does not know how to find the check whose guess is close to the real speed of the car. He does not trust his father as he thinks that he might cheat. Can you help to resolve this issue between them by telling whose guess is closer. If Sebi's guess is better, output "SEBI". If his father's guess is better, output "FATHER". If both the guess are equally close, then output "DRAW". -----Input----- The first line of the input contains an integer T denoting the number of test cases. Each of the next T lines contain five space separated integers S, SG, FG, D, T corresponding to the Sebi's car speed, Sebi's guess, his father's guess, D as defined in the statement and the time at which both the cars at against the markers (in seconds), respectively. -----Output----- Output description. For each test case, output a single line containing "SEBI", "FATHER" or "DRAW" (without quotes) denoting whose guess is better. -----Constraints----- - 1 ≤ T ≤ 10000 - 0 ≤ S ≤ 130 - 0 ≤ SG, FG ≤ 300 - 1 ≤ D ≤ 30 - 1 ≤ T ≤ 300 - The other car speed doesn't exceed 300 kph. -----Example----- Input: 2 100 180 200 20 60 130 131 132 1 72 Output: SEBI FATHER -----Explanation----- Example case 1. There are total 20 - 1 = 19 markers in between the Sebi's car and the other car. So, the distance between those cars at time T is 20 * 50 = 1000 meters = 1 km. As T = 60 seconds, i.e. 1 minutes. So, the other car goes 1 km more than Sebi's car in 1 minute. So, the other car will go 60 km more than Sebi's car in 1 hour. So, its speed is 60 kmph more than Sebi's car, i.e. 160 kmph. Sebi had made a guess of 180 kmph, while his father of 200 kmph. Other car's real speed is 160 kmph. So, Sebi's guess is better than his father. Hence he wins the game. Example case 2. The situation of this example is depicted in the image provided in the statement. You can find the speed of other car and see that Father's guess is more accurate. [
876
W3HIXP6DFM74
Bob has got some injury in his leg and due to this he can take exactly M steps in one move. Bob enters a square field of size NxN. The field is only having one gate(for both entrance and exit) at its one of the corners. Bob started walking along the perimeter of square field.(remember Bob can only take exactly M steps in one move and cannot reverse his direction of motion). Bob wants to know how many minimum number of moves he needs to come out(i.e. he reaches the same gate from where he entered into the field) from the square field. Tell the answer to Bob ASAP. Luckily, you came to know M=N+1. -----Input----- - The first line of the input contains an integer T denoting the number of test cases. - Each test case contains a single integer N denoting the sides of the square. -----Output----- - For each test case, output a single line containing minimum number of moves Bob required to come out from the field. -----Constraints----- - 1 ≤ T ≤ 10000 - 1 ≤ N ≤ 1000000000 -----Example----- Input: 2 1 2 Output: 2 8 -----Explanation----- Example case 1.Let four corners of square be (0,0), (0,1), (1,1), (1,0). Let gate be at (0,0). Bob takes 2 steps in one move. Let movement of Bob be as follows (0,0) -> (1,1) -> (0,0). Thus minimum moves needed were 2. Example case 2.Let four corners of square be (0,0), (0,2), (2,2), (2,0). Let gate be at (0,0). Bob takes 3 steps in one move. Let movement of Bob be as follows (0,0) -> (2,1) -> (0,2) -> (1,0) -> (2,2) -> (0,1) -> (2,0) -> (1,2) -> (0,0). Thus minimum number of moves needed are 8. [
450
B69GZFPD9AL7
Chef has a sequence of positive integers $A_1, A_2, \ldots, A_N$. He wants to choose some elements of this sequence (possibly none or all of them) and compute their MEX, i.e. the smallest positive integer which does not occur among the chosen elements. For example, the MEX of $[1, 2, 4]$ is $3$. Help Chef find the largest number of elements of the sequence $A$ which he can choose such that their MEX is equal to $M$, or determine that it is impossible. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the maximum number of elements Chef can choose, or $-1$ if he cannot choose elements in such a way that their MEX is $M$. -----Constraints----- - $1 \le T \le 100$ - $2 \le M \le N \le 10^5$ - $1 \le A_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^6$ -----Example Input----- 1 3 3 1 2 4 -----Example Output----- 3 -----Explanation----- Example case 1: The MEX of whole array is 3. Hence, we can choose all the elements. [
371
9L8N9NTS6N5V
"Humankind cannot gain anything without first giving something in return. To obtain, something of equal value must be lost. That is alchemy's first law of Equivalent Exchange. In those days, we really believed that to be the world's one, and only truth." -- Alphonse Elric Now, here we have an equivalent exchange law for triangles which states that two right-angled isosceles triangles of the same color can be made into a square of the same color using Alchemy. You are given N$N$ right-angled isosceles colored triangles numbered from 1$1$ to N$N$. For each triangle, the two equal sides have a length of 1$1$ unit. The Color of i$i$-th triangle is given by Ci$C_i$. To create a tower, we choose some consecutive (2×k)+1$2 \times k)+1$ triangles for any k≥0$k \geq 0$. We then pick some 2×k$2 \times k$ of them (these need not be consecutive), and form k$k$ pairs of triangles such that both triangles in pair have the same color. Also, each of the 2×k$2 \times k$ should be in exactly one pair. Then the two triangles in each pair are joined using Alchemy (following the law of equivalent exchange for triangles) to form squares and these k$k$ squares are placed one upon other. The one remaining triangle is placed as a roof to the tower. This results in a tower of the height of k$k$. Find the maximum height of the tower that can be formed. In other words, you should select the largest consecutive segment of triangles, such that you can form a tower using every single one of those triangles. In particular, you leave out one triangle, which will form the roof, and the other triangles should all be paired up such that both triangles in a pair have the same colour. -----Input:----- - The first line contains T$T$, the number of test cases. Then the test cases follow. - For every test case, the first line contains N$N$ denoting the number of triangles. - For every test case, the second line contains N$N$ space-separated integers Ci$C_{i}$ denoting the color of the triangles. ( 1≤i≤N$1 \leq i \leq N$). -----Output:----- For every test case, output a single integer denoting the maximum height of the tower that can be formed. -----Constraints----- - 1≤T≤100$1 \leq T \leq 100$ - 1≤N≤105$1 \leq N \leq 10^{5}$ - 1≤Ci≤30$1 \leq C_{i} \leq 30$ - Sum of N$N$ over all test cases doesn't exceed 5×105$5\times 10^{5}$ -----Sample Input:----- 4 14 5 4 2 2 3 2 1 3 2 7 4 9 9 9 3 1 2 1 3 1 1 1 5 1 2 3 4 1 -----Sample Output:----- 3 1 1 0 -----EXPLANATION:----- - #1$1$: The subarray [2,2,3,2,1,3,2]$[2, 2, 3, 2, 1, 3, 2]$ results in a tower of height 3$3$. - #2$2$: The subarray [1,2,1]$[ 1, 2, 1 ]$ results in a tower of height 1$1$. - #3$3$: The subarray [1,1,1]$[ 1, 1, 1 ]$ results in a tower of height 1$1$. - #4$4$: The subarrays [1]$[ 1 ]$, [2]$[ 2 ]$ , [3]$[ 3 ]$, [4]$[ 4 ]$ and [1]$[ 1 ]$ all results in a tower of height 0$0$. The above tower is possible by subarray [2,2,3,2,1,3,2]$[2, 2, 3, 2, 1, 3, 2]$ resulting in a height of 3$3$ in test case 1$1$. [
982
SR1B3WTBNPD9
Chef has just finished the construction of his new garden. He has sown the garden with patches of the most beautiful carpet grass he could find. He has filled it with patches of different color and now he wants to evaluate how elegant his garden is. Chef's garden looks like a rectangular grid of cells with N rows and M columns. So there are N x M cells in total. In each cell Chef planted grass of some color. The elegance of the garden is defined by the number of squares, composed of at least four garden cells, with edges parallel to the sides of the garden, that have four corner cells of the same color. Given the description of Chef's garden, calculate how many such squares exist. Input format The first line contains the number T, the number of test cases. In the following lines, T test cases follow (without any newlines between them.) The first line of each test case contains N and M, separated by a single space. Each of the next N lines contains M characters without any spaces between them, and without any leading or trailing spaces. Each character describes the color of the corresponding cell in the garden and belongs to the set of lowercase and uppercase lettes of the English alphabet. One letter in lowercase and uppercase describes different colors. Output format For each test case, print the number of squares that conform to the definition in the problem statement. Constraints 1 ≤ T ≤ 50 1 ≤ N, M ≤ 50 Sample input 3 2 2 aa aA 3 3 aba bab aba 4 4 aabb aabb bbaa bbaa Sample output 0 1 4 Explanation In the first case the only avaliable square does not conform to the definition in the problem statement because 'a' and 'A' describes different colors. In the second case, you can select the 4 a's at the corners of the garden. In the third case, you can only make four squares, from the four 2x2 segments that are of the same color. [
431
FQHK6CMMEHPL
Ram and Shyam are playing a game of Truth and Dare. In this game, Shyam will ask Ram to perform tasks of two types: - Truth task: Ram has to truthfully answer a question. - Dare task: Ram has to perform a given task. Each task is described by an integer. (If a truth task and a dare task are described by the same integer, they are still different tasks.) You are given four lists of tasks: - $T_{r, 1}, T_{r, 2}, \dots, T_{r, t_r}$: the truth tasks Ram can perform. - $D_{r, 1}, D_{r, 2}, \dots, D_{r, d_r}$: the dare tasks Ram can perform. - $T_{s, 1}, T_{s, 2}, \dots, T_{s, t_s}$: the truth tasks Shyam can ask Ram to perform. - $D_{s, 1}, D_{s, 2}, \dots, D_{s, d_s}$: the dare tasks Shyam can ask Ram to perform. Note that the elements of these lists are not necessarily distinct, each task may be repeated any number of times in each list. Shyam wins the game if he can find a task Ram cannot perform. Ram wins if he performs all tasks Shyam asks him to. Find the winner of the game. Let's take an example where Ram can perform truth tasks $3$, $2$ and $5$ and dare tasks $2$ and $100$, and Shyam can give him truth tasks $2$ and $3$ and a dare task $100$. We can see that whichever truth or dare tasks Shyam asks Ram to perform, Ram can easily perform them, so he wins. However, if Shyam can give him dare tasks $3$ and $100$, then Ram will not be able to perform dare task $3$, so Shyam wins. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $t_r$. - The second line contains $t_r$ space-separated integers $T_{r, 1}, T_{r, 2}, \dots, T_{r, t_r}$. - The third line contains a single integer $d_r$. - The fourth line contains $d_r$ space-separated integers $D_{r, 1}, D_{r, 2}, \dots, D_{r, d_r}$. - The fifth line contains a single integer $t_s$. - The sixth line contains $t_s$ space-separated integers $T_{s, 1}, T_{s, 2}, \dots, T_{s, t_s}$. - The seventh line contains a single integer $d_s$. - The eighth line contains $d_s$ space-separated integers $D_{s, 1}, D_{s, 2}, \dots, D_{s, d_s}$. -----Output----- For each test case, print a single line containing the string "yes" if Ram wins the game or "no" otherwise. -----Constraints----- - $1 \le T \le 100$ - $1 \le t_r, d_r, t_s, d_s \le 100$ - $1 \le T_{r, i} \le 100$ for each valid $i$ - $1 \le D_{r, i} \le 100$ for each valid $i$ - $1 \le T_{s, i} \le 100$ for each valid $i$ - $1 \le D_{s, i} \le 100$ for each valid $i$ -----Example Input----- 4 2 1 2 3 1 3 2 1 2 2 3 2 2 1 2 3 1 3 2 1 2 3 3 2 4 3 3 2 5 2 2 100 1 2 1 100 2 1 2 3 1 3 2 1 2 3 3 2 2 -----Example Output----- yes no yes yes -----Explanation----- Example case 1: Ram's truth tasks are $[1, 2]$ and his dare tasks are $[1, 3, 2]$. Shyam's truth tasks are $[2]$ and his dare tasks are $[3, 2]$. Ram can perform all tasks Shyam gives him. Example case 2: Ram's truth tasks are $[1, 2]$ and his dare tasks are $[1, 3, 2]$. Shyam's truth tasks are $[2]$ and his dare tasks are $[3, 2, 4]$. If Shyam asks Ram to perform dare task $4$, Ram will not be able to do it. [
1,085
1A33MAAMHVO2
An encoder encodes the first $16$ lowercase English letters using $4$ bits each. The first bit (from the left) of the code is $0$ if the letter lies among the first $8$ letters, else it is $1$, signifying that it lies among the last $8$ letters. The second bit of the code is $0$ if the letter lies among the first $4$ letters of those $8$ letters found in the previous step, else it's $1$, signifying that it lies among the last $4$ letters of those $8$ letters. Similarly, the third and the fourth bit each signify the half in which the letter lies. For example, the letter $j$ would be encoded as : - Among $(a,b,c,d,e,f,g,h$ $|$ $i,j,k,l,m,n,o,p)$, $j$ appears in the second half. So the first bit of its encoding is $1$. - Now, among $(i,j,k,l$ $|$ $m,n,o,p)$, $j$ appears in the first half. So the second bit of its encoding is $0$. - Now, among $(i,j$ $|$ $k,l)$, $j$ appears in the first half. So the third bit of its encoding is $0$. - Now, among $(i$ $|$ $j)$, $j$ appears in the second half. So the fourth and last bit of its encoding is $1$. So $j$'s encoding is $1001$, Given a binary encoded string $S$, of length at most $10^5$, decode the string. That is, the first 4 bits are the encoding of the first letter of the secret message, the next 4 bits encode the second letter, and so on. It is guaranteed that the string's length is a multiple of 4. -----Input:----- - The first line of the input contains an integer $T$, denoting the number of test cases. - The first line of each test case contains an integer $N$, the length of the encoded string. - The second line of each test case contains the encoded string $S$. -----Output:----- For each test case, print the decoded string, in a separate line. -----Constraints----- - $1 \leq T \leq 10$ - $4 \leq N \leq 10^5$ - The length of the encoded string is a multiple of $4$. - $0 \le S_i \le 1$ -----Subtasks----- - $100$ points : Original constraints. -----Sample Input:----- 3 4 0000 8 00001111 4 1001 -----Sample Output:----- a ap j -----Explanation:----- - Sample Case $1$ : The first bit is $0$, so the letter lies among the first $8$ letters, i.e., among $a,b,c,d,e,f,g,h$. The second bit is $0$, so it lies among the first four of these, i.e., among $a,b,c,d$. The third bit is $0$, so it again lies in the first half, i.e., it's either $a$ or $b$. Finally, the fourth bit is also $0$, so we know that the letter is $a$. - Sample Case $2$ : Each four bits correspond to a character. Just like in sample case $1$, $0000$ is equivalent to $a$. Similarly, $1111$ is equivalent to $p$. So, the decoded string is $ap$. - Sample Case $3$ : The first bit is $1$, so the letter lies among the last $8$ letters, i.e., among $i,j,k,l,m,n,o,p$. The second bit is $0$, so it lies among the first four of these, i.e., among $i,j,k,l$. The third bit is $0$, so it again lies in the first half, i.e., it's either $i$ or $j$. Finally, the fourth bit is $1$, so we know that the letter is $j$. [
880
T4HX9EGQCNUQ
Chef wants to serve mankind by making people immortal by preparing a dish, a dish of life - a dish with the best taste in the universe, one with the smell and splash of fresh water flowing down the springs of the mountain, one with the smell of the best lily flowers of the garden, one that has contained the very essence of life in a real sense. This dish will contain K ingredients that are found only in remote islands amid mountains. For sake of convenience, we enumerate the ingredients by the integers from 1 to K, both inclusive. There are N islands and each of them offers some ingredients. Chef being a little child did not know how to collect the ingredients for the recipe. He went to all the islands and bought all the ingredients offered in each island. Could he possibly have saved some time by skipping some island? If it was not possible for Chef to collect the required ingredients (i.e. all the K ingredients), output "sad". If it was possible for him to skip some islands, output "some", otherwise output "all". -----Input----- First line of the input contains an integer T denoting number of test cases. The description of T test cases follow. The first line of each test case contains two space separated integers N, K. The i-th of the next lines will contain first an integer Pi, denoting the number of ingredients grown in the i-th island, followed by Pi distinct integers in the range [1, K]. All the integers are space separated. -----Output----- For each test case, output a single line containing one of the strings "sad", "all" or "some" (without quotes) according to the situation. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N, K ≤ 105 - 1 ≤ Pi ≤ K - Sum of Pi over all test cases ≤ 106 -----Subtasks----- Subtask #1 (30 points) - 1 ≤ N, K ≤ 50 Subtask #2 (30 points) - 1 ≤ K ≤ 50 Subtask #3 (40 points) - original constraints -----Example----- Input 3 3 4 3 1 2 3 2 1 3 2 1 2 2 3 3 1 2 3 2 1 3 2 3 2 1 2 2 1 3 Output sad some all -----Explanation----- Example 1. The ingredient 4 is not available in any island, so Chef can't make the dish of life. Hence, the answer is "sad". Example 2. Chef can just go to the first island and collect all the three ingredients required. He does not need to visit the second island. So, the answer is "some". Example 3. Chef has to visit both the islands in order to obtain all the three ingredients. So, the answer is "all". [
614
DSCEU811D1LK
Motu and Patlu are playing with a Magical Ball. Patlu find some interesting pattern in the motion of the ball that ball always bounce back from the ground after travelling a linear distance whose value is some power of $2$. Patlu gave Motu total distance $D$ travelled by the ball and ask him to calculate the minimum number of bounces that the ball makes before coming to rest. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, single integers $D$. - Note : Power of $2$ must be a non-negative integer. -----Output:----- For each testcase, output in a single line answer, the minimum number of bounces the ball makes before coming to rest. -----Constraints----- - $1 \leq T \leq 10^5$ - $1$ $\leq$ $M$< $10$^18 -----Sample Input:----- 1 13 -----Sample Output:----- 2 -----EXPLANATION:----- [
225
4PO9B6W0K935
Two players are playing a game. The game is played on a sequence of positive integer pairs. The players make their moves alternatively. During his move the player chooses a pair and decreases the larger integer in the pair by a positive multiple of the smaller integer in the pair in such a way that both integers in the pair remain positive. If two numbers in some pair become equal then the pair is removed from the sequence. The player who can not make any move loses (or in another words the player who encounters an empty sequence loses). Given the sequence of positive integer pairs determine whether the first player can win or not (assuming that both players are playing optimally). -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test starts with an integer N denoting the number of pairs. Each of the next N lines contains a pair of positive integers. -----Output----- For each test case, output a single line containing "YES" if the first player can win and "NO" otherwise. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N ≤ 100 - All other integers are between 1 to 108 - The integers in each pair will be different -----Example----- Input: 3 1 2 3 2 4 5 5 6 2 2 3 3 5 Output: NO NO YES -----Explanation----- Example case 1. The first player don't have any choice other subtracting 2 from 3. So during the turn of the second player integer pair will be (2,1). The second player will win by subtracting 1 from 2. Example case 2. If the first player choose to move (4,5) to (4,1) the second player will make it to (1,1). If the first player choose to move (5,6) to (5,1) the second player will make it to (1,1). So regardless of the move of the first player, the second will always win. Example case 3. The first player will select pair (3,5) and make it to (3,2). Now both pairs are equal. So whatever the move of second player he will just mirror that move in another pair. This will ensure his win. [
495
YK6HO2PFOC0Y
The chef is trying to solve some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- * * * * *** *** * * *** *** ***** ***** * * *** *** ***** ***** ******* ******* -----EXPLANATION:----- No need, else pattern can be decode easily. [
181
0L16TV7EQQZ2
In a fictitious city of CODASLAM there were many skyscrapers. The mayor of the city decided to make the city beautiful and for this he decided to arrange the skyscrapers in descending order of their height, and the order must be strictly decreasing but he also didn’t want to waste much money so he decided to get the minimum cuts possible. Your job is to output the minimum value of cut that is possible to arrange the skyscrapers in descending order. -----Input----- *First line of input is the number of sky-scrappers in the city *Second line of input is the height of the respective sky-scrappers -----Output----- * Your output should be the minimum value of cut required to arrange these sky-scrappers in descending order. -----Example----- Input: 5 1 2 3 4 5 Output: 8 By: Chintan,Asad,Ashayam,Akanksha [
195
URTZF6C2522G
There is Chef and Chef’s Crush who are playing a game of numbers. Chef’s crush has a number $A$ and Chef has a number $B$. Now, Chef wants Chef’s crush to win the game always, since she is his crush. The game ends when the greatest value of A^B is reached after performing some number of operations (possibly zero), Where ^ is Bitwise XOR. Before performing any operation you have to ensure that both $A$ and $B$ have the same number of bits without any change in the values. It is not guaranteed that $A$ and $B$ should have same number of bits in the input. For example, if $A$ is $2$ and $B$ is $15$, then the binary representation of both the numbers will have to be $0010$ and $1111$ respectively, before performing any operation. The operation is defined as : - Right circular shift of the bits of only $B$ from MSB$_B$ to LSB$_B$ i.e. if we consider $B_1 B_2 B_3 B_4$ as binary number, then after one circular right shift, it would be $B_4 B_1 B_2 B_3$ They both are busy with themselves, can you find the number of operations to end the game? -----Input :----- - The first line of input contains $T$, (number of test cases) - Then each of the next $T$ lines contain : two integers $A$ and $B$ respectively. -----Output :----- For each test case print two space-separated integers, The number of operations to end the game and value of A^B when the game ends. -----Constraints :----- - $1 \leq T \leq100$ - $1\leq A,B \leq 10^{18}$ -----Subtasks :----- - 30 Points: $1\leq A,B \leq 10^5$ - 70 Points: Original Constraints -----Sample Input :----- 1 4 5 -----Sample Output :----- 2 7 -----Explanation :----- Binary representation of $4$ is $100$ and binary representation $5$ is $101$. - After operation $1$ : $B$ $=$ $110$, so A^B $=$ $2$ - After operation $2$ : $B$ $=$ $011$, so A^B $=$ $7$ So, the value of A^B will be $7$. Which is the greatest possible value for A^B and the number of operations are $2$. [
558
WZOUXAH4Z1EH
Chef Tobby is playing a rapid fire with Bhuvan. He gives Bhuvan a string S and each time, Bhuvan has to guess whether there exists 2 equal subsequences in the string or not. Bhuvan got a perfect score in the game with Chef Tobby. However, Chef Tobby has now asked Bhuvan to write a program that will do this automatically given a string S. Bhuvan is an intelligent man but he does not know how to write a code. Can you help him? Find two different subsequences such that they are equal in their value, more formally, find two sequences of indices (a1, a2, ..., ak-1, ak) and (b1, b2, ..., bk-1, bk) such that: - 1≤ ai, bi ≤ |S| - ai < ai+1 for all valid i - bi < bi+1 for all valid i - Sai = Sbi for all valid i - there exist at least one i such that ai is not equal to bi -----Input section----- The first line contains T, the number of test cases. Each of the next T lines contain one string S each. Input will only consist of lowercase english characters -----Output section----- For each test case, output "yes" or "no" (without quotes) as the solution to the problem. -----Input constraints----- 1 ≤ T ≤ 1000 1 ≤ length of S ≤ 100 -----Sample Input----- 4 likecs venivedivici bhuvan codechef -----Sample Output----- no yes no yes -----Explanation----- In test case 2, one of the possible equal subsequence is "vi" and "vi". (one at position {0, 3} and other at {4, 7}, assuming 0-based indexing). In test case 4, one of the possible equal subsequence is "ce" and "ce". (one at position {0, 3} and other at {4, 6}, assuming 0-based indexing). [
438
DI3LBRB8JV6C
Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants his dad to give him a few items. Chef does not want to burden his son. But he won't stop bothering him unless he is given a few items to carry. So Chef decides to give him some items. Obviously, Chef wants to give the kid less weight to carry. However, his son is a smart kid. To avoid being given the bare minimum weight to carry, he suggests that the items are split into two groups, and one group contains exactly K items. Then Chef will carry the heavier group, and his son will carry the other group. Help the Chef in deciding which items should the son take. Your task will be simple. Tell the Chef the maximum possible difference between the weight carried by him and the weight carried by the kid. -----Input:----- The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow. The first line of each test contains two space-separated integers N and K. The next line contains N space-separated integers W1, W2, ..., WN. -----Output:----- For each test case, output the maximum possible difference between the weights carried by both in grams. -----Constraints:----- - 1 ≤ T ≤ 100 - 1 ≤ K < N ≤ 100 - 1 ≤ Wi ≤ 100000 (105) -----Example:----- Input: 2 5 2 8 4 5 2 10 8 3 1 1 1 1 1 1 1 1 Output: 17 2 -----Explanation:----- Case #1: The optimal way is that Chef gives his son K=2 items with weights 2 and 4. Chef carries the rest of the items himself. Thus the difference is: (8+5+10) − (4+2) = 23 − 6 = 17. Case #2: Chef gives his son 3 items and he carries 5 items himself. [
458
U6J2520SOMBB
Consider the following operations on a triple of integers. In one operation, you should: - Choose an integer $d$ and an arithmetic operation ― either addition or multiplication. - Choose a subset of elements of the triple. - Apply the arithmetic operation to each of the chosen elements, i.e. either add $d$ to each of them or multiply each of them by $d$. For example, if we have a triple $(3, 5, 7)$, we may choose to add $3$ to the first and third element, and we get $(6, 5, 10)$ using one operation. You are given an initial triple $(p, q, r)$ and a target triple $(a, b, c)$. Find the minimum number of operations needed to transform $(p, q, r)$ into $(a, b, c)$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains three space-separated integers $p$, $q$ and $r$. - The second line contains three space-separated integers $a$, $b$ and $c$. -----Output----- For each test case, print a single line containing one integer ― the minimum required number of operations. -----Constraints----- - $1 \le T \le 1,000$ - $|p|, |q|, |r|, |a|, |b|, |c| \le 10^9$ -----Subtasks----- Subtask #1 (10 points): $|p|, |q|, |r|, |a|, |b|, |c| \le 10$ Subtask #2 (90 points): original constraints -----Example Input----- 2 3 5 7 6 5 10 8 6 3 9 7 8 -----Example Output----- 1 2 -----Explanation----- Example case 1: We add $3$ to the first and third element of $(3, 5, 7)$ to form $(6, 5, 10)$. Example case 2: We can add $1$ to each element to form $(9, 7, 4)$ and then multiply the third element by $2$. [
494
W21LV15GLYX3
Johnny was asked by his math teacher to compute nn (n to the power of n, where n is an integer), and has to read his answer out loud. This is a bit of a tiring task, since the result is probably an extremely large number, and would certainly keep Johnny occupied for a while if he were to do it honestly. But Johnny knows that the teacher will certainly get bored when listening to his answer, and will sleep through most of it! So, Johnny feels he will get away with reading only the first k digits of the result before the teacher falls asleep, and then the last k digits when the teacher wakes up. Write a program to help Johnny to compute the digits he will need to read out. -----Input----- The first line contains t, the number of test cases (about 30000). Then t test cases follow. Each test case consists of one line containing two numbers n and k (1 ≤ n ≤ 109, 1 ≤ k ≤ 9). It is guaranteed that k is not more than the number of digits of nn. -----Output----- For each test case, print out one line containing two numbers, separated by a space, which are the first and the last k digits of nn. -----Example----- Input 2 4 2 9 3 Output 25 56 387 489 [
276
D88P0PRY1L2P
Sherlock Holmes has decided to start a new academy to some of the young lads. He has conducted several tests and finally selected N equally brilliant students.Now he don't know whether to train all the N students or not. Now since Holmes was in a confusion, Watson came up with an idea. He wanted to test the obedience of the students. So during the camp, the students were given some Swiss Chocolates as gifts each time when they passed a level.Now some of them have finished eating all the chocolates, some of them had some remaining. Now to test their team chemistry and IQ skills, Watson told the lads to arrange themselves in such a way that, number of chocolates of the ith kid should be equal to the sum of (i-1)th kid and (i-2)th kid. Now they have arranged themselves in an order. Now Sherlock announced that he will select the students who have formed the line according to this order. But since there can be many such small groups among the entire N kids, he will select a sequence of kids such that the length of the sequence is maximized, meanwhile satisfying the above condition -----Input----- First line is an integer T which denotes the total number of test cases. Each of the next T lines contains an integer N which denotes, N students. The next line contains N spaced integers.where it denotes the order in which the kids arranged themselves. -----Output----- Each line contains an integer which denotes the maximum number of students among the N students who have arranged themselves according the rule said by Watson.It is guaranteed that Holmes will select atleast 1 or 2 students -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 10^5 - 1 ≤ Each of next N integers ≤ 10^9 -----Subtasks----- Subtask #1 : (20 points) - 1 ≤ T ≤ 10 - 1 ≤ N≤ 100 - 1 ≤ Each element≤ 10^3 Subtask 2 : (80 points) - 1 ≤ T ≤ 10 - 1 ≤ N≤ 100000 - 1 ≤ Each element≤ 10^9 -----Example----- Input: 2 5 2 3 5 1 2 3 1 2 3 Output: 3 3 -----Explanation----- Example case 1. Here the first kid has 2 chocolates, second has 3 chocolates, third kid has 5 chocolates, which is the sum of first kid's total chocolates and second kid's chocolate. Forth student has only 1 chocolate where he did not follow the rule. So the maximum number of kids who arranged themselves in the order was 3. That is students at index 1 to index 3. [
590
1EJMAQYXSIPB
A robot is initially at $(0,0)$ on the cartesian plane. It can move in 4 directions - up, down, left, right denoted by letter u, d, l, r respectively. More formally: - if the position of robot is $(x,y)$ then u makes it $(x,y+1)$ - if the position of robot is $(x,y)$ then l makes it $(x-1,y)$ - if the position of robot is $(x,y)$ then d makes it $(x,y-1)$ - if the position of robot is $(x,y)$ then r makes it $(x+1,y)$ The robot is performing a counter-clockwise spiral movement such that his movement can be represented by the following sequence of moves - ulddrruuulllddddrrrruuuuu… and so on. A single move takes 1 sec. You have to find out the position of the robot on the cartesian plane at $t$ second. -----Input:----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $t$. -----Output:----- For each test case, print two space-separated integers, $(x,y)$ — the position of the robot. -----Constraints----- - $1 \leq T \leq 10^6$ - $1 \leq t \leq 10^{18}$ -----Sample Input:----- 5 1 2 3 50 12233443 -----Sample Output:----- 0 1 -1 1 -1 0 2 4 -1749 812 [
363
4X2PUQY0MRCI
Prof. Sergio Marquina is a mathematics teacher at the University of Spain. Whenever he comes across any good question(with complexity k), he gives that question to students within roll number range i and j. At the start of the semester he assigns a score of 10 to every student in his class if a student submits a question of complexity k, his score gets multiplied by k. This month he gave M questions and he is wondering what will be mean of maximum scores of all the students. He is busy planning a tour of the Bank of Spain for his students, can you help him? -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains the first line of input, two integers N, M i.e. Number of students in the class and number of questions given in this month. - Next M lines contain 3 integers -i,j,k i.e. starting roll number, end roll number, and complexity of the question -----Output:----- - For each test case, output in a single line answer - floor value of Mean of the maximum possible score for all students. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N, M \leq 105$ - $1 \leq i \leq j \leq N$ - $1 \leq k \leq 100$ -----Sample Input:----- 1 5 3 1 3 5 2 5 2 3 4 7 -----Sample Output:----- 202 -----EXPLANATION:----- Initial score of students will be : [10,10,10,10,10] after solving question 1 scores will be: [50,50,50,10,10] after solving question 2 scores will be: [50,100,100,20,20] after solving question 1 scores will be: [50,100,700,140,20] Hence after all questions mean of maximum scores will (50+100+700+140+20)/5=202 [
441
3NT3LB8ZVOE1
The city of Siruseri is impeccably planned. The city is divided into a rectangular array of cells with $M$ rows and $N$ columns. Each cell has a metro station. There is one train running left to right and back along each row, and one running top to bottom and back along each column. Each trains starts at some time $T$ and goes back and forth along its route (a row or a column) forever. Ordinary trains take two units of time to go from one station to the next. There are some fast trains that take only one unit of time to go from one station to the next. Finally, there are some slow trains that take three units of time to go from one station the next. You may assume that the halting time at any station is negligible. Here is a description of a metro system with $3$ rows and $4$ columns: $ $ S(1) F(2) O(2) F(4) F(3) . . . . S(2) . . . . O(2) . . . . $ $ The label at the beginning of each row/column indicates the type of train (F for fast, O for ordinary, S for slow) and its starting time. Thus, the train that travels along row 1 is a fast train and it starts at time $3$. It starts at station ($1$, $1$) and moves right, visiting the stations along this row at times $3, 4, 5$ and $6$ respectively. It then returns back visiting the stations from right to left at times $6, 7, 8$ and $9$. It again moves right now visiting the stations at times $9, 10, 11$ and $12$, and so on. Similarly, the train along column $3$ is an ordinary train starting at time $2$. So, starting at the station ($3$,$1$), it visits the three stations on column $3$ at times $2, 4$ and $6$, returns back to the top of the column visiting them at times $6,8$ and $10$, and so on. Given a starting station, the starting time and a destination station, your task is to determine the earliest time at which one can reach the destination using these trains. For example suppose we start at station ($2$,$3$) at time $8$ and our aim is to reach the station ($1$,$1$). We may take the slow train of the second row at time $8$ and reach ($2$,$4$) at time $11$. It so happens that at time $11$, the fast train on column $4$ is at ($2$,$4$) travelling upwards, so we can take this fast train and reach ($1$,$4$) at time $12$. Once again we are lucky and at time $12$ the fast train on row $1$ is at ($1$,$4$), so we can take this fast train and reach ($1$, $1$) at time $15$. An alternative route would be to take the ordinary train on column $3$ from ($2$,$3$) at time $8$ and reach ($1$,$3$) at time $10$. We then wait there till time $13$ and take the fast train on row $1$ going left, reaching ($1$,$1$) at time $15$. You can verify that there is no way of reaching ($1$,$1$) earlier than that. -----Input:----- The first line contains two integers $M$ and $N$ indicating the number rows and columns in the metro system. This is followed by $M$ lines, lines $2, 3, …, M+1$, describing the trains along the $M$ rows. The first letter on each line is either F or O or S, indicating whether the train is a fast train, an ordinary train or a slow train. Following this, separated by a blank space, is an integer indicating the time at which this train starts running. The next $N$ lines, lines $M+2, M+3, …, N+M+1$, contain similar descriptions of the trains along the $N$ columns. The last line, line $N+M+2$, contains $5$ integers $a, b, c, d$ and $e$ where ($a$,$b$) is the starting station, $c$ is the starting time and ($d$,$e$) is the destination station. -----Output:----- A single integer indicating the earliest time at which one may reach the destination. -----Constraints:----- - $1 \leq M, N \leq 50$. - $1 \leq a, d \leq M$ - $1 \leq b, e \leq N$ - $1 \leq$ all times in input $\leq 20$ -----Sample Input----- 3 4 F 3 S 2 O 2 S 1 F 2 O 2 F 4 2 3 8 1 1 -----Sample Output----- 15 [
1,108
IQKEGR1BGON5
Taxis of Kharagpur are famous for making sharp turns. You are given the coordinates where a particular taxi was on a 2-D planes at N different moments: (x1, y1), (x2, y2), ..., (xN, yN). In between these coordinates, the taxi moves on a straight line. A turn at the i-th (2 ≤ i ≤ N-1) coordinate is said to be a sharp turn if the angle by which it turns at Point B = (xi, yi) when going from coordinates A = (xi-1, yi-1) to C = (xi+1, yi+1) via (xi, yi) is greater than 45 degrees. ie. suppose you extend the line segment AB further till a point D, then the angle DBC would be greater than 45 degrees. You have to identify whether the taxi made a sharp turn somewhere or not (Please look at Output section for details). If it made a sharp turn, also identify whether it is possible to change the coordinates at one of the N moments to make sure that the taxi doesn't make any sharp turn. Note that all the N pairs of coordinates (including the new coordinates) should be integers and distinct and should have their x and y coordinates at least 0 and at most 50. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N denoting the number of moments at which you are given the information of the taxi's coordinates. - Each of the next N lines contains two space-separated integers xi and yi denoting the x and y coordinates of the taxi at i-th moment. -----Output----- - For each test case, print a single line containing two space-separated strings, either of which can be a "yes" or "no" (without quotes). If there was no sharp turn, the first string should be "yes", and if there was a sharp turn, the first string should be "no". For the second string, you should tell whether it is possible to modify at most one coordinate in such a way that taxi doesn't make a sharp turn. Note that if the first string is "yes", then the second string would always be "yes". -----Constraints----- - 1 ≤ T ≤ 50 - 3 ≤ N ≤ 50 - 0 ≤ xi, yi ≤ 50 - It's guaranteed that all (xi, yi) pairs are distinct. -----Example----- Input 5 3 0 0 1 1 2 1 3 0 0 1 0 6 1 3 0 0 1 0 1 1 4 0 0 1 0 1 1 6 1 6 0 0 1 0 1 1 2 1 2 2 3 2 Output yes yes yes yes no yes no yes no no -----Explanation----- Example 1. You can see that taxi is never making a sharp turn. Example 3 You can see that taxi is making a sharp turn of 90 degrees, an angle greater than 45'. However, you can change the coordinates of the third points to (2, 1) to ensure that the angle remains <= 45'. [
716
T1N8Y8AUFB5M
Chef is the judge of a competition. There are two players participating in this competition — Alice and Bob. The competition consists of N races. For each i (1 ≤ i ≤ N), Alice finished the i-th race in Ai minutes, while Bob finished it in Bi minutes. The player with the smallest sum of finish times wins. If this total time is the same for Alice and for Bob, a draw is declared. The rules of the competition allow each player to choose a race which will not be counted towards their total time. That is, Alice may choose an index x and her finish time in the race with this index will be considered zero; similarly, Bob may choose an index y and his finish time in the race with this index will be considered zero. Note that x can be different from y; the index chosen by Alice does not affect Bob's total time or vice versa. Chef, as the judge, needs to announce the result of the competition. He knows that both Alice and Bob play optimally and will always choose the best option. Please help Chef determine the result! -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - The second line contains N space-separated integers A1, A2, ..., AN. - The third line contains N space-separated integers B1, B2, ..., BN. -----Output----- For each test case, print a single line containing the string "Alice" if Alice wins, "Bob" if Bob wins or "Draw" if the result is a draw (without quotes). -----Constraints----- - 1 ≤ T ≤ 100 - 2 ≤ N ≤ 100 - 1 ≤ Ai ≤ 1000 for each valid i - 1 ≤ Bi ≤ 1000 for each valid i -----Example----- Input: 3 5 3 1 3 3 4 1 6 2 5 3 5 1 6 2 5 3 3 1 3 3 4 3 4 1 3 2 2 7 Output: Alice Bob Draw -----Explanation----- Example case 1: Alice will choose the finish time in the last race to be considered zero, which means her sum of finish times is 3 + 1 + 3 + 3 + 0 = 10, while Bob will choose the finish time of his second race to be considered zero, so his total sum of finish times is 1 + 0 + 2 + 5 + 3 = 11. Since Alice's sum is smaller, she is considered the winner. Example case 2: We're dealing with the same situation as in the previous case, but finish times for the players are swapped, so Bob wins this time. Example case 3: Alice will choose the finish time of the first race to be considered zero, which means her total time is 0 + 1 + 3 = 4. Bob will choose the finish time of his last race to be considered zero, which makes his total time 2 + 2 + 0 = 4. The competition is considered a draw because both players have equal sums of finish times. [
688
RNSDWYMNNN7Y
This time minions are celebrating Diwali Festival. There are N minions in total. Each of them owns a house. On this Festival, Each of them wants to decorate their house. But none of them have enough money to do that. One of the minion, Kevin, requested Gru for money. Gru agreed for money distribution but he will be giving money to a minion if and only if demanded money is less than or equal to the money Gru have. Now Gru wonders if he can spend all the money or not. -----Input----- First line have number of test cases T. Each test case consist of Two Lines. First line contains two space separated integers N and K i.e. Number of minions and Amount of Money Gru have. Next line contains N space separated integers A1,A2,A3,.....,AN representing amount of money demanded by ith minion. -----Output----- Output YES if Gru can spend his all of the money on minions i.e. after distribution Gru have zero amount of money else NO. -----Constraints----- - 1 ≤ T ≤ 105 - 1 ≤ N ≤ 102 - 1 ≤ K,Ai ≤ 109 -----Example----- Input: 2 4 9 5 2 2 4 4 9 5 2 18 3 Output: YES NO -----Explanation----- Example case 1.At first Gru is having 9 Rs. If he gives 5 Rs. to first minion then remaining 4 Rs. can be given to 2nd and 3rd minion or to the 4th minion. Which will leave zero amount of money in the hands of Gru. Example case 2.At first Gru is having 9 Rs. If he gives 5 Rs. to the first minion then from remaining 4 Rs. either he can give 2 Rs. to the 2nd minion or 3 Rs. to the fourth minion. Which will leave either 2 Rs. or 1 Rs. in the hands of Gru. [
423
LLAGD4BBH0UE
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have N≠M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≤i≤Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer — the maximum size of a correct sub-board. -----Constraints----- - 1≤N,M≤200$1 \le N, M \le 200$ - 1≤Q≤105$1 \le Q \le 10^5$ - 0≤ci≤109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≤N,M≤20$1 \le N, M \le 20$ - 1≤Q≤100$1 \le Q \le 100$ Subtask #2 (30 points): 1≤N,M≤20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer. [
759
8IDA9D4J8LL4
The land of Programmers Army is surrounded by many islands. A unique number is associated with each island. The king of the islands is a very generous person, he donates a certain amount of gold coins to travelers for visiting each island that they visited to. Now, you are appointed as a traveler, who will travel to all these(or some) islands as many times as the Army wants, and you will collect gold coins from the king of the island. In each trip, you will be asked to give the total sum of gold coins you have collected. -----Input:----- - The first line of the input contains a single integer $T$. $T$ denoting the number of test cases. The description of $T$ test cases is as follows. - The next line of the input contains a single integer $N$. $N$ denotes the total number of Islands. - The next line of the input contains $N$ space-separated integers $A1, A2, A3...An$ where $ith$ number denotes the maximum number of coins that the king of $ith$ island can donate. - Next line contains a single integer $Q$. $Q$ denotes the total number of times traveler have to go for the trip. - Next $Q$ lines contains, two space-separated integers $Q1,Q2$ denoting the start and end number of islands, i.e. traveler will start the trip from $Q1th$ island and will go till $Q2th$ island, in each trip. Note: islands are numbered from $1$ to $N$. -----Output:----- - For each trip print the total number of gold coins, traveler will collect(each on a new line). -----Constraints:----- - $1 \leq T \leq 100$ - $1 \leq N \leq 10^4$ - $1 \leq A1, A2, A3...An \leq 10^5$ - $1 \leq Q \leq 10^3$ - $1 \leq Q1,Q2 \leq N$ -----Sample Input:----- 1 4 10 2 5 50 2 1 3 2 4 -----Sample Output:----- 17 57 -----Explanation:----- - In 1st Trip, traveler will go from 1st Island to 3rd Island, hence the total number of coins traveler can collect is 10+2+5 = 17 - In 2 d Trip, traveler will go from 2nd Island to 4th Island, hence the total number of coins traveler can collect is 2+5+50 = 57 [
559
Z6TF74JM6MVS
-----Coal Company ----- The Tunisian Coal Mining company uses a train to ferry out coal blocks from its coal mines. The train has N containers numbered from 1 to N which need to be filled with blocks of coal. Assume there are infinite coal blocks. The containers are arranged in increasing order of capacity, and the ith container has capacity i. Every container has a specific loading cost ci. The workers fill the containers in rounds. In every round, they choose a subset of containers and load them with coal blocks. This subset should be such that each subsequent container chosen in a round should be more spacious than the previous one. Also, the difference in loading cost of consecutive containers should be at least K. What is the least number of rounds in which all containers can be filled? ----- Input ----- The first line contains the number of test cases T. T test cases follow. Each case contains an integer N and K on the first line, followed by integers c1,...,cn on the second line. 1 <= T <= 100 1 <= N <= 300 1 <= ci <= 1000 1 <= K <= 1000 ----- Output ----- Output T lines, one for each test case, containing the minimum number of rounds in which all containers could be filled. ----- Example ----- Input: 2 3 2 5 4 7 5 1 5 3 4 5 6 Output: 2 1 Explanation: For the first example, workers can fill the containers of cost 5 and 7 in the first round and the container with cost 4 in the next round. Note that the containers with cost 5 and 4 cannot be filled consecutively because the loading costs should differ by at least K (which is 2). Also, the containers cannot be filled in order 5, 7, 4 in one round because the containers filled in a round should be in increasing capacity. [
407
3YOH6ZZZQM7R
"If you didn't copy assignments during your engineering course, did you even do engineering?" There are $Q$ students in Chef's class. Chef's teacher has given the students a simple assignment: Write a function that takes as arguments an array $A$ containing only unique elements and a number $X$ guaranteed to be present in the array and returns the ($1$-based) index of the element that is equal to $X$. The teacher was expecting a linear search algorithm, but since Chef is such an amazing programmer, he decided to write the following binary search function: integer binary_search(array a, integer n, integer x): integer low, high, mid low := 1 high := n while low ≤ high: mid := (low + high) / 2 if a[mid] == x: break else if a[mid] is less than x: low := mid+1 else: high := mid-1 return mid All of Chef's classmates have copied his code and submitted it to the teacher. Chef later realised that since he forgot to sort the array, the binary search algorithm may not work. Luckily, the teacher is tired today, so she asked Chef to assist her with grading the codes. Each student's code is graded by providing an array $A$ and an integer $X$ to it and checking if the returned index is correct. However, the teacher is lazy and provides the exact same array to all codes. The only thing that varies is the value of $X$. Chef was asked to type in the inputs. He decides that when typing in the input array for each code, he's not going to use the input array he's given, but an array created by swapping some pairs of elements of this original input array. However, he cannot change the position of the element that's equal to $X$ itself, since that would be suspicious. For each of the $Q$ students, Chef would like to know the minimum number of swaps required to make the algorithm find the correct answer. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $Q$ denoting the number of elements in the array and the number of students. - The second line contains $N$ space-separated integers $A_1, A_2, \dots, A_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $X$. -----Output----- For each query, print a single line containing one integer — the minimum required number of swaps, or $-1$ if it is impossible to make the algorithm find the correct answer. (Do you really think Chef can fail?) -----Constraints----- - $1 \le T \le 10$ - $1 \le N, Q \le 10^5$ - $1 \le A_i \le 10^9$ for each valid $i$ - $1 \le X \le 10^9$ - all elements of $A$ are pairwise distinct - for each query, $X$ is present in $A$ - sum of $N$ over all test cases $\le 5\cdot10^5$ - sum of $Q$ over all test cases $\le 5\cdot10^5$ -----Subtasks----- Subtask #1 (20 points): $1 \le N \le 10$ Subtask #2 (30 points): - $1 \le A_i \le 10^6$ for each valid $i$ - $1 \le X \le 10^6$ Subtask #3 (50 points): original constraints -----Example Input----- 1 7 7 3 1 6 7 2 5 4 1 2 3 4 5 6 7 -----Example Output----- 0 1 1 2 1 0 0 -----Explanation----- Example case 1: - Query 1: The algorithm works without any swaps. - Query 2: One solution is to swap $A_2$ and $A_4$. - Query 3: One solution is to swap $A_2$ and $A_6$. - Query 4: One solution is to swap $A_2$ with $A_4$ and $A_5$ with $A_6$. - Query 5: One solution is to swap $A_2$ and $A_4$. - Query 6: The algorithm works without any swaps. - Query 7: The algorithm works without any swaps. [
987
8Z7W6GCB3M9Y
Chef likes strings a lot but he likes palindromic strings more. Today, Chef has two strings A and B, each consisting of lower case alphabets. Chef is eager to know whether it is possible to choose some non empty strings s1 and s2 where s1 is a substring of A, s2 is a substring of B such that s1 + s2 is a palindromic string. Here '+' denotes the concatenation between the strings. Note: A string is a palindromic string if it can be read same both forward as well as backward. To know more about palindromes click here. -----Input----- - First line of input contains a single integer T denoting the number of test cases. - For each test case: - First line contains the string A - Second line contains the string B. -----Output----- For each test case, Print "Yes" (without quotes) if it possible to choose such strings s1 & s2. Print "No" (without quotes) otherwise. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ |A|, |B| ≤ 1000 -----Subtasks----- - Subtask 1: 1 ≤ |A|, |B| ≤ 10 : ( 40 pts ) - Subtask 2: 1 ≤ |A|, |B| ≤ 1000 : ( 60 pts ) -----Example-----Input 3 abc abc a b abba baab Output Yes No Yes -----Explanation----- - Test 1: One possible way of choosing s1 & s2 is s1 = "ab", s2 = "a" such that s1 + s2 i.e "aba" is a palindrome. - Test 2: There is no possible way to choose s1 & s2 such that s1 + s2 is a palindrome. - Test 3: You can figure it out yourself. [
415
C9BRONCTQ4YJ
Chef's new hobby is painting, but he learned the fact that it's not easy to paint 2D pictures in a hard way, after wasting a lot of canvas paper, paint and of course time. From now on, he decided to paint 1D pictures only. Chef's canvas is N millimeters long and is initially all white. For simplicity, colors will be represented by an integer between 0 and 105. 0 indicates white. The picture he is envisioning is also N millimeters long and the ith millimeter consists purely of the color Ci. Unfortunately, his brush isn't fine enough to paint every millimeter one by one. The brush is 3 millimeters wide and so it can only paint three millimeters at a time with the same color. Painting over the same place completely replaces the color by the new one. Also, Chef has lots of bottles of paints of each color, so he will never run out of paint of any color. Chef also doesn't want to ruin the edges of the canvas, so he doesn't want to paint any part beyond the painting. This means, for example, Chef cannot paint just the first millimeter of the canvas, or just the last two millimeters, etc. Help Chef by telling him whether he can finish the painting or not with these restrictions. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N. The second line contains N space-separated integers C1, C2, ..., CN denoting the colors of Chef's painting. -----Output----- For each test case, output a single line containing either “Yes” or “No” (without quotes), denoting whether Chef can finish the painting or not. -----Constraints----- - 1 ≤ T ≤ 105 - 3 ≤ N ≤ 105 - The sum of the Ns over all the test cases in a single test file is ≤ 5×105 - 1 ≤ Ci ≤ 105 -----Example----- Input:3 4 1 5 5 5 4 1 1 1 5 3 5 5 2 Output:Yes Yes No -----Explanation----- Example case 1. Chef's canvas initially contains the colors [0,0,0,0]. Chef can finish the painting by first painting the first three millimeters with color 1, so the colors become [1,1,1,0], and then the last three millimeters with color 5 so that it becomes [1,5,5,5]. Example case 2. Chef's canvas initially contains the colors [0,0,0,0]. Chef can finish the painting by first painting the last three millimeters by color 5 so the colors become [0,5,5,5], and then the first three millimeters by color 1 so it becomes [1,1,1,5]. Example case 3. In this test case, Chef can only paint the painting as a whole, so all parts must have the same color, and the task is impossible. [
657
8TL95HNB5JTQ
Kefaa has developed a novel decomposition of a tree. He claims that this decomposition solves many difficult problems related to trees. However, he doesn't know how to find it quickly, so he asks you to help him. You are given a tree with $N$ vertices numbered $1$ through $N$. Let's denote an edge between vertices $u$ and $v$ by $(u, v)$. The triple-tree decomposition is a partition of edges of the tree into unordered triples of edges $(a, b), (a, c), (a, d)$ such that $a \neq b \neq c \neq d$. Each edge must belong to exactly one triple. Help Kefaa with this problem — find a triple-tree decomposition of the given tree or determine that no such decomposition exists. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - Each of the following $N-1$ lines contains two space-separated integers $u$ and $v$ describing an edge between vertices $u$ and $v$ of the tree. -----Output----- - For each test case, print a line containing the string "YES" if a triple-tree decomposition of the given tree exists or "NO" otherwise. - If it exists, print $\frac{N-1}{3}$ more lines describing a decomposition. - Each of these lines should contain four space-separated integers $a$, $b$, $c$ and $d$ describing a triple of edges $(a, b), (a, c), (a, d)$. If more than one triple-tree decomposition exists, you may output any one. -----Constraints----- - $1 \le T \le 100$ - $2 \le N \le 2 \cdot 10^5$ - $1 \le u, v \le N$ - the sum of $N$ over all test cases does not exceed $2 \cdot 10^5$ -----Subtasks----- Subtask #1 (20 points): $2 \le N \le 10$ Subtask #2 (30 points):$2 \le N \le 5000$ and the sum of $N$ overall testcases doesn't exceed $5000$ Subtask #3 (50 points): original constraints -----Example Input----- 2 4 1 2 1 3 1 4 7 1 2 2 3 1 4 4 5 1 6 6 7 -----Example Output----- YES 1 2 3 4 NO [
569
CU3RQI1JUWWI
Sandu, a teacher in Chefland introduced his students to a new sequence i.e. 0,1,0,1,2,0,1,2,3,0,1,2,3,4........ The Sequence starts from 0 and increases by one till $i$(initially i equals to 1), then repeat itself with $i$ changed to $i+1$ Students being curious about the sequence asks the Nth element of the sequence. Help Sandu to answer the Students -----Input:----- - The first-line will contain $T$, the number of test cases. Then the test case follows. - Each test case contains a single numbers N. -----Output:----- Print the Nth element of the sequence -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq N \leq 10^{18}$ -----Sample Input:----- 5 8 9 20 32 109 -----Sample Output:----- 2 3 5 4 4 [
222
HV5J6IWIRVL3
Ayu loves distinct letter sequences ,a distinct letter sequence is defined by a sequence of small case english alphabets such that no character appears more then once. But however there are two phrases that she doesn't like these phrases are "kar" and "shi" and she is given a sequence of distinct characters and she wonders how many such sequences she can form using all the characters such that these phrases don't occur. Help her finding the number of such sequences. New Year Gift - It is guaranteed that for sequences of length greater then 6 letters k,a,r,s,h,i will be present(we thought of being generous, thank us later :)). -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each line consists of a string $S$ (3<=s.length<=18) of distinct characters. -----Output:----- Print the number of sequences that can be formed by permuting all the characters such that phrases "kar" and "shi" don't occur. -----Constraints----- - $1 \leq T \leq 10$ - $3 \leq S.length \leq 18$ -----Sample Input:----- 2 karp abcd -----Sample Output:----- 22 24 [
263
DTUSBQEA8C6C
The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredients, but makes less food. The chef, however, does not like fractions. The original recipe contains only whole numbers of ingredients, and the chef wants the reduced recipe to only contain whole numbers of ingredients as well. Help the chef determine how much of each ingredient to use in order to make as little food as possible. -----Input----- Input will begin with an integer T, the number of test cases. Each test case consists of a single line. The line begins with a positive integer N, the number of ingredients. N integers follow, each indicating the quantity of a particular ingredient that is used. -----Output----- For each test case, output exactly N space-separated integers on a line, giving the quantity of each ingredient that the chef should use in order to make as little food as possible. -----Sample Input----- 3 2 4 4 3 2 3 4 4 3 15 9 6 -----Sample Output----- 1 1 2 3 4 1 5 3 2 -----Constraints----- T≤100 2≤N≤50 All ingredient quantities are between 1 and 1000, inclusive. [
292
XQWBMCPGIQPE
Today is Chef's birthday. His mom has surprised him with truly fruity gifts: 2 fruit baskets. The first basket contains N apples, and the second one contains M oranges. Chef likes apples and oranges very much but he likes them equally, and therefore, wants to have the minimum possible difference between the number of apples and oranges he has. To do so, he can purchase 1 apple or 1 orange by paying exactly 1 gold coin (that's some expensive fruit, eh?). Chef can purchase fruits at most K times (as he has only K gold coins in his pocket) to make the difference the minimum possible. Our little Chef is busy in celebrating his birthday to the fullest, and therefore, he has handed this job to his best friend — you. Can you help him by finding the minimum possible difference he can achieve between the number of apples and orange he owns? -----Input----- The first line of input contains a single integer T denoting the number of test cases. The first and only line of each test case contains 3 space separated integers — N, M and K — denoting the number of apples, number of oranges, and number of gold coins our little Chef has. -----Output----- For each test case, output the minimum possible difference between the number of apples and oranges that Chef can achieve. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N, M, K ≤ 100 -----Example-----Input 3 3 4 1 5 2 1 3 4 3 Output 0 2 0 -----Explanation----- - Test 1: Chef will buy 1 apple by paying 1 gold coin and will have equal number of apples and oranges. - Test 2: Chef will buy 1 orange by paying 1 gold coin and will have 5 apples and 3 oranges. [
385
5YIIUV3NW39R
After acquiring an extraordinary amount of knowledge through programming contests, Malvika decided to harness her expertise to train the next generation of Indian programmers. So, she decided to hold a programming camp. In the camp, she held a discussion session for n members (n-1 students, and herself). They are sitting in a line from left to right numbered through 1 to n. Malvika is sitting in the nth spot. She wants to teach m topics of competitive programming to the students. As the people taking part in the camp are all newbies, they know none of the topics being taught, i.e., initially, the first n - 1 people in the line know none of the topics, while the nth knows all of them. It takes one hour for a person to learn a topic from his neighbour. Obviously, one person cannot both teach a topic as well as learn one during the same hour. That is, in any particular hour, a person can either teach a topic that he knows to one of his neighbors, or he can learn a topic from one of his neighbors, or he can sit idly. It is also obvious that if person x is learning from person y at a particular hour, then person y must be teaching person x at that hour. Also, note that people can work parallely too, i.e., in the same hour when the 4th person is teaching the 3rd person, the 1st person can also teach the 2nd or learn from 2nd. Find out the minimum number of hours needed so that each person learns all the m topics. -----Input----- - The first line of input contains a single integer T denoting number of test cases. - The only line of each test case contains two space separated integers n, m as defined in the statement. -----Output----- - For each test case, output a single integer in a line corresponding to the answer of the problem. -----Constraints----- - 1 ≤ T, n, m ≤ 100 -----Example----- Input: 2 2 1 3 2 Output: 1 4 -----Explanation----- In the first example, there are two people. Second person is Malvika and she has to teach only one topic to the first person. It will take only one hour to do so. In the second example, there are three people. The 3rd person is Malvika and she has to teach only two topics to 1st and 2nd person. In the 1st hour, she teaches the 1st topic to the 2nd person. Now, in the 2nd hour, the 2nd person will teach the 1st topic to the 1st person. In the 3rd hour, Malvika will teach the 2nd topic to the 2nd person. Now the 2nd person will teach that topic to the 1st in the 4th hour. So, it takes a total of 4 hours for all the people to know all the topics. [
625
RMCHVMQW7PSQ
Find out the maximum sub-array of non negative numbers from an array. The sub-array should be continuous. That is, a sub-array created by choosing the second and fourth element and skipping the third element is invalid. Maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is greater than sub-array B if sum(A) > sum(B). NOTE 1 :If there is a tie, then compare with segment's length and return segment which has maximum length NOTE 2: If there is still a tie, then return the segment with minimum starting index. -----Input----- The first line contains the number of test cases. Each test cases contains an integer N. next line consists of N integers, the elements of the array. -----Output----- Print out the maximum sub-array as stated above. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N ≤ 105 - 1 ≤ Ai ≤ 105 -----Example----- Input: 1 6 1 2 5 -7 2 3 Output: 1 2 5 [
244
S252N8971RLP
Chef found a strange string yesterday - a string of signs s, where each sign is either a '<', '=' or a '>'. Let N be the length of this string. Chef wants to insert N + 1 positive integers into this sequence and make it valid. A valid sequence is a sequence where every sign is preceded and followed by an integer, and the signs are correct. That is, if a sign '<' is preceded by the integer a and followed by an integer b, then a should be less than b. Likewise for the other two signs as well. Chef can take some positive integers in the range [1, P] and use a number in the range as many times as he wants. Help Chef find the minimum possible P with which he can create a valid sequence. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains the string of signs s, where each sign is either '<', '=' or a '>'. -----Output----- For each test case, output a single line containing an integer corresponding to the minimum possible P. -----Constraints----- - 1 ≤ T, |s| ≤ 105 - 1 ≤ Sum of |s| over all test cases in a single test file ≤ 106 -----Subtasks----- Subtask #1 (30 points) - 1 ≤ T, |s| ≤ 103 - 1 ≤ Sum of |s| over all test cases in a single test file ≤ 104 Subtask #2 (70 points) - Original constraints -----Example----- Input: 4 <<< <>< <=> <=< Output: 4 2 2 3 -----Explanation----- Here are some possible valid sequences which can be formed with the minimum P for each of the test cases: 1 < 2 < 3 < 4 1 < 2 > 1 < 2 1 < 2 = 2 > 1 1 < 2 = 2 < 3 [
430
32LVOX0SR4YN
You came across this story while reading a book. Long a ago when the modern entertainment systems did not exist people used to go to watch plays in theaters, where people would perform live in front of an audience. There was a beautiful actress who had a disability she could not pronounce the character $'r'$. To win her favours which many have been denied in past, you decide to write a whole play without the character $'r'$. Now you have to get the script reviewed by the editor before presenting it to her. The editor was flattered by the script and agreed to you to proceed. The editor will edit the script in this way to suit her style. For each word replace it with a sub-sequence of itself such that it contains the character 'a'. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements Wikipedia Now given a script with $N$ words, for each word in the script you wish to know the number of subsequences with which it can be replaced. -----Input:----- - First-line will contain $N$, the number of words in the script. Then next $N$ line with one test case each. - Each test case contains a single word $W_i$ -----Output:----- For each test case, output in a single line number of subsequences with which it can be replaced. -----Constraints----- - $1 \leq N \leq 1000$ - $1 \leq$ length of $W_i$ $\leq 20$ - $W_i$ on contains lowercase english alphabets and does not have the character 'r' -----Sample Input 1:----- 2 abc aba -----Sample Output 1:----- 4 6 -----EXPLANATION:----- This subsequences with which $abc$ can be replaed : ${a,ab,ac,abc}$. This subsequences with which $aba$ can be replaed : ${a,ab,aba,a,ba,a}$. -----Sample Input 2:----- 3 abcde abcdea xyz -----Sample Output 2:----- 16 48 0 [
460
A858M1X4L29M
Not everyone probably knows that Chef has younder brother Jeff. Currently Jeff learns to read. He knows some subset of the letter of Latin alphabet. In order to help Jeff to study, Chef gave him a book with the text consisting of N words. Jeff can read a word iff it consists only of the letters he knows. Now Chef is curious about which words his brother will be able to read, and which are not. Please help him! -----Input----- The first line of the input contains a lowercase Latin letter string S, consisting of the letters Jeff can read. Every letter will appear in S no more than once. The second line of the input contains an integer N denoting the number of words in the book. Each of the following N lines contains a single lowecase Latin letter string Wi, denoting the ith word in the book. -----Output----- For each of the words, output "Yes" (without quotes) in case Jeff can read it, and "No" (without quotes) otherwise. -----Constraints----- - 1 ≤ |S| ≤ 26 - 1 ≤ N ≤ 1000 - 1 ≤ |Wi| ≤ 12 - Each letter will appear in S no more than once. - S, Wi consist only of lowercase Latin letters. -----Subtasks----- - Subtask #1 (31 point): |S| = 1, i.e. Jeff knows only one letter. - Subtask #2 (69 point) : no additional constraints -----Example----- Input:act 2 cat dog Output:Yes No -----Explanation----- The first word can be read. The second word contains the letters d, o and g that aren't known by Jeff. [
355
4JX44QSZ6Q8K
Chef has a calculator which has two screens and two buttons. Initially, each screen shows the number zero. Pressing the first button increments the number on the first screen by 1, and each click of the first button consumes 1 unit of energy. Pressing the second button increases the number on the second screen by the number which is currently appearing on the first screen. Each click of the second button consumes B units of energy. Initially the calculator has N units of energy. Now chef wonders what the maximum possible number is, that he gets on the second screen of the calculator, with the limited energy. -----Input----- The first line of the input contains an integer T denoting the number of test cases. Each test case is described using a single line containing two integers, N and B. -----Output----- For each test case, output a single line containing the answer to this test case. -----Constraints----- - 1 ≤ T ≤ 10,000 - 1 ≤ N, B ≤ 1,000,000,000 -----Subtasks----- - Subtask 1 (20 points): 1 ≤ N, B ≤ 1,000 - Subtask 2 (80 points): Original constraints -----Example----- Input: 3 10 2 8 5 6 1 Output: 12 3 9 -----Explanation----- Example case 1. There are 10 units of energy available. Pressing second button takes 2 units of energy. Chef can achieve 12 on the second screen as follows. - Press first button to get scores (1, 0). 9 units of energey is left. - Press first button to get scores (2, 0). 8 units of energy remaining. - Press first button to get scores (3, 0). 7 units of energy remaining. - Press first button to get scores (4, 0). 6 units of energy remaining. - Press second button to get scores (4, 4). 4 units of energy remaining. - Press second button to get scores (4, 8). 2 units of energy remaining. - Press second button to get scores (4, 12). 0 units of energy remaining. [
459
IG0Y3TGAZJ4M
Chef has a circular sequence $A$ of $N$ non-negative integers $A_1, A_2, \ldots, A_N$ where $A_i$ and $A_{i+1}$ are considered adjacent, and elements $A_1$ and $A_N$ are considered adjacent. An operation on position $p$ in array $A$ is defined as replacing $A_p$ by the bitwise OR of elements adjacent to $A_p$. Formally, an operation is defined as: - If $p = 1$, replace $A_1$ with $A_N | A_{2}$ - If $1 < p < N$, replace $A_p$ with $A_{p-1} | A_{p+1}$ - If $p = N$, replace $A_N$ with $A_{N-1} | A_1$ Here, '|' denotes the bitwise OR operation. Now, Chef must apply operations at each position exactly once, but he may apply the operations in any order. Being a friend of Chef, you are required to find a sequence of operations, such that after applying all the $N$ operations, the bitwise OR of the resulting array is $K$, or determine that no such sequence of operations exist. -----Input:----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $K$ denoting the number of elements, and the required bitwise OR after operations. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output:----- For each test case, if no valid sequence of operations exist, print -1. Otherwise, print $N$ space-separated integers in which $i$-th integer denote the position chosen in the $i$-th operation. If there are multiple valid sequences of operations, you may print any valid sequence. -----Constraints----- - $1 \le T \le 10^5$ - $3 \le N \le 2*10^5$ - $0 \le A_i, K \le 10^9$ - It's guaranteed that the total length of the arrays in one test file doesn't exceed $10^6$ -----Sample Input:----- 5 3 6 2 1 6 3 6 2 1 5 3 7 2 4 6 3 7 1 2 4 3 7 1 2 6 -----Sample Output:----- 2 1 3 -1 -1 -1 2 3 1 -----Explanation:----- In the first test case, initially, the sequence is {2, 1, 6}. - Chef applies the operation on the $2^{nd}$ index, so the sequence converts to {2, 6, 6}. - Next, Chef applies the operation on the $1^{st}$ index, so the sequence converts to {6, 6, 6}. - Next, Chef applies the operation on the $3^{rd}$ index, and this time sequence does not change. The final sequence is {6, 6, 6}, and bitwise OR of this sequence is $6$ that is equal to given $K$. [
721
5B39KBL6IOZX
The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K(odd) to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 3 5 7 -----Sample Output:----- 1 111 111 111 11111 11 11 1 1 1 11 11 11111 1111111 11 11 1 1 1 1 1 1 1 1 1 1 1 11 11 1111111 -----EXPLANATION:----- No need, else pattern can be decode easily. [
233
1B7J4MVUZHKC
Humpy, the little elephant, has his birthday coming up. He invited all his cousins but doesn’t know how many of them are really coming as some of them are having exams coming up. He will only get to know how many of them are coming on the day of his birthday. He ordered sugarcane for his party, of length L. Humpy’s mom decided that she will be dividing the sugarcane among Humty and his friends in a way such that they get the sugarcane in ratio of their ages. Your task is to determine whether it is possible to serve sugarcane to everyone as integral multiples of their ages. -----INPUT----- First line of input contains an integer N, denoting the number of test cases. Then N test cases follow. The first line of each test case contains three integers K, L and E. K denoting the number of friends coming; L denoting the length of the sugarcane and E denoting the age of the little elephant. Next line has K space separated integers denoting the age of friends who came to the party. -----OUTPUT----- For each test case, output “YES” (without quotes) if everyone gets their part as integral multiples of their ages; otherwise output “NO”(without quotes). -----CONSTRAINTS----- - 1 <= T<=30 - 1 <= K<=1000 - 1 <= L<=1000000 - 1 <= E<=100000 - 1 <= Age of Cousins<=100000 -----Example----- Input: 2 4 10 2 2 2 3 1 4 12 3 6 5 7 3 Output: YES NO [
354
EIAR3097TTYY
You may have tried your level best to help Chef but Dr Doof has managed to come up with his masterplan in the meantime. Sadly, you have to help Chef once again. Dr Doof has designed a parenthesis-inator. It throws a stream of $N$ brackets at the target, $1$ bracket per second. The brackets can either be opening or closing. Chef appears in front of the stream at time $t$. If Chef faces an opening bracket, he gets hit. However, if he faces a closing bracket, he may choose to let it pass through him (Chef is immune to closing brackets). Chef gets a chance to counter attack Doof as soon as he finds a balanced non-empty bracket sequence. Help Chef by providing him the minimum time $x$ at which he will be able to launch his counter attack. If Chef is unable to counter attack, answer $-1$. Formally, you are given a string $S$ of length $N$ consisting only of opening brackets $($ and closing brackets $)$. The substring of $S$ starting at index $L$ and ending at index $R$, i.e. $S_L S_{L+1} \ldots S_{R}$ is denoted by $S[L, R]$ . Consider $Q$ cases. In the $i^{\text{th}}$ case, Chef appears at time $t_i$ $(1 \leq t_i \leq N)$ and faces all characters from index $t_i$ to $N$. Find the minimum index $x$ $(t_i \leq x \leq N)$ such that the substring $S[t_i, x]$ contains a non-empty balanced bracket subsequence containing the same number of opening brackets as $S[t_i, x]$ (i.e., you cannot remove any opening bracket from the substring). If such an $x$ does not exist, print $-1$. A string $X$ is called a subsequence of a string $Y$ if it is possible to obtain $X$ by erasing some (possibly zero) characters from $Y$ without changing the order of the remaining characters. A balanced bracket sequence is defined as: - an empty string is a balanced bracket sequence. - if $s$ is a balanced bracket sequence, then so is $(s)$. - if $s$ and $t$ are balanced bracket sequences, then so is $st$. $Note :-$ The input files are large. The use of Fast I/O is recommended. -----Input----- - The first line contains a single integer $T$ denoting the number of testcases. - The first line of each test case contains the string $S$. - The next line contains a single integer $Q$ denoting the number of cases to consider. - The next line contains $Q$ space separated integers, each denoting $t_i$. -----Output----- For each query, print the minimum value of $x$ in a separate line. If no such $x$ exists, print $-1$. -----Constraints----- - $1 \leq T \leq 10^3$ - $1 \leq |S| \leq 10^7$ - $1 \leq Q \leq 10^6$ - $1 \leq t_i \leq N$ - Every character of $S$ is either $($ or $)$. - Sum of $|S|$ and $Q$ over all testcases for a particular test file does not exceed $10^7$ and $10^6$ respectively. -----Sample Input----- 1 )())((() 3 1 7 6 -----Sample Output----- 3 8 -1 -----Explanation----- For the first query, Chef chooses to let $S_1$ pass through him, gets hit by $S_2$ and finally completes a balanced bracket sequence by adding $S_3$ to $S_2$ at time $x$ = $3$. [
832
DROX2NQV06CY
Let's call a sequence good if the sum of all its elements is $0$. You have a sequence of integers $A_1, A_2, \ldots, A_N$. You may perform any number of operations on this sequence (including zero). In one operation, you should choose a valid index $i$ and decrease $A_i$ by $i$. Can you make the sequence good using these operations? -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing the string "YES" if it is possible to make the given sequence good or "NO" if it is impossible. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 10$ - $|A_i| \le 100$ for each valid $i$ -----Subtasks----- Subtask #1 (10 points): $N = 1$ Subtask #2 (30 points): $N \le 2$ Subtask #3 (60 points): original constraints -----Example Input----- 2 1 -1 2 1 2 -----Example Output----- NO YES -----Explanation----- Example case 2: We can perform two operations ― subtract $1$ from $A_1$ and $2$ from $A_2$. [
351
E2QBM78OX83V
Given a binary string $S$ consisting of only 1’s and 0’s where 1 represents a Square and 0 represents a Circle. The diameter of the circle and the side of the square must be any integer (obviously > 0) . You will have to perfectly inscribe (as shown in the example below) the respective geometric figure at $S$$i+1$ inside of $S$$i$ where $i$ $\epsilon$ $[0,N-2]$, if it is possible. Note that, it will not be possible to inscribe if the dimension of the geometric figure you are perfectly inscribing is not an integer and you will discard the rest of the string. Find the maximum number of circles we can inscribe in a square according to the given string. For a given binary string there can be only one geometric figure and this figure is concentric. For example : the string 1100 can be represented as the figure below, the first two squares have the same side length and the next two circles have the same diameter. Another example : the string 0001 can be represented as the one given below Again here, we have 3 circles of the same diameter and one square inscribed in them. -----Input:----- The first line contains $N$, the number of strings Then each of the next $N$ lines contains a binary string $S$. -----Output:----- The $N$ lines of output should have $N$ integers in separate lines, the maximum number of circles we can inscribe in a square according to the given string $S$ . -----Constraints----- - 1 $\leq$ $N$ $\leq$ 103 - 1 $\leq$ length of string $S$ $\leq$ 104 -----Sample Input:----- 3 1110 0010 1001000 -----Sample Output:----- 1 0 2 -----Explanation:----- In the first case, we can inscribe the string 1110 as : three squares of side length 4 units (on top of each other) and then we can inscribe one circle of diameter 4 units. The answer is 1 since, there is 1 circle inscribed in a square. In the second case 0010, Let the first two circles be of some diameter 10, we can see that we cannot inscribe another square of any integer dimension inside them. So, the answer is 0. In the third case 1001000, we can take the first square of size 10, then inscribe two circles of diameter 5, then we cannot inscribe another square in this since, it will not be of any possible integer dimension and we discard the rest of the string. [
577
1HLHRW9PNHF0
You are an evil sorcerer at a round table with $N$ sorcerers (including yourself). You can cast $M$ spells which have distinct powers $p_1, p_2, \ldots, p_M$. You may perform the following operation any number of times (possibly zero): - Assign a living sorcerer to each positive integer cyclically to your left starting from yourself ― the closest living sorcerer to your left is assigned to $1$, the next living sorcerer to the left is assigned to $2$ and so on. Note that each living sorcerer (including yourself) is assigned to an infinite number of integers. - Choose a spell $j$ (possibly a spell you have chosen before) and kill the living sorcerer assigned to $p_j$. You may not cast a spell to kill yourself. What is the maximum number of sorcerers you can kill using zero or more operations? -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - The second line contains $M$ space-separated integers $p_1, p_2, \ldots, p_M$. -----Output----- For each test case, print a single line containing one integer ― the maximum number of sorcerers you can kill. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 10^9$ - $1 \le M \le 3 \cdot 10^5$ - $1 \le p_i \le 10^9$ for each valid $i$ - $p_1, p_2, \ldots, p_N$ are pairwise distinct - the sum of $M$ over all test cases does not exceed $3 \cdot 10^5$ -----Example Input----- 5 4 1 5 6 2 2 4 1 4 7 16 8 29 1000000000 1 998244353 1 1 20201220 -----Example Output----- 3 4 0 1755647 0 -----Explanation----- Example case 1: The initial state is shown in the figure from the statement. We can first use spell $1$ and kill the $5$-th sorcerer to our left, i.e. sorcerer $2$. Now there are $3$ living sorcerers and the state is as shown in the following figure: We can use spell $1$ again and kill the current $5$-th living sorcerer to our left, i.e. sorcerer $4$. Now there are $2$ living sorcerers and the state is: Finally, we can use spell $1$ again and kill the only other living sorcerer, i.e. sorcerer $3$. Now, none of the other sorcerers are alive. As we cannot cast a spell to kill ourselves, we cannot improve the answer any further. Example case 2: We can perform $4$ operations using the spell $p_1 = 2$ each time. We can also instead use $p_2 = 4$ in the first two operations and $p_1 = 2$ in the last two operations. Note that there may be multiple valid sequences of operations that lead to the best answer. Example case 3: We cannot perform any operations using any of the given spells, so we are unable to kill any sorcerers. Example case 4: We can perform $1,755,647$ operations, each of them using the spell $p_1 = 998,244,353$. [
793
UMUG9A8PLJOK
Teacher Sungjae wanted to hold a programming competition for his students where every participant need to be included into team. The participants submitted their team names before the deadline. After the competition ran for half an hour, (It is assured that each registered team will submit absolutely once within half an hour) Sungjae mistakenly pressed a button that changed the order of the registered team names. Now in the submission list, order of the characters in the team's name doesn't matter. That means $abc$, $acb$, $bac$, $bca$, $cab$, $cba$ refers to the same team. The competition ran for two hours and then ended. Sungjae now counting each of the team's score and wants to print the registered team names and score. The scoreboard should be ordered based on scores in decreasing order and if two teams have same score, Sangjae would follow lexicographical order. $N$.$B$. frequency of each character's in a registered team's name will not match with another team. That means two teams named $xoxo$ and $oxox$ is not possible. Because both of them have the same frequency of each of the characters (two 'o' and two 'x'). Similarly $abb$ and $bab$ is not possible (because both of them have one 'a' and two 'b'). It is ensured that only possible test cases will be given. -----Input:-----Input: - First line will contain $T$, number of testcases. Then the testcases follow. - The first line of each test case contains two integers , $N$ and $R$ - total number of submissions and the number of submissions within first half an hour. - Then $R$ lines follow: the i'th line contains a string $ti$, registered names of the teams and an integer $pi$, points they got on that submission. - Then $N-R$ lines follow: the i-th line contains a string $ti$- the i-th team's name (in any order) in lowercase letter only and $pi$ -points they got on that submission. -----Output:-----Output: For each testcase,print the scoreboard. That means print the teams name and their point according to their score in decreasing order and if some of them have same score,print the teams name in lexicographical order -----Constraints-----Constraints - $1 \leq T \leq 10$ - $1 \leq R \leq N \leq 1000$ - $1 \leq ti \leq 1000$ - $1 \leq pi \leq 10^6$ Sum of points ($pi$) of a team will not cross $10^9$. -----Sample Input:-----Sample Input: 1 10 5 amigoes 1 bannermen 1 monarchy 4 outliers 5 iniciador 10 aegimos 2 iiiacdnor 1 eilorstu 1 gimosae 3 mnachroy 7 -----Sample Output:-----Sample Output: iniciador 11 monarchy 11 amigoes 6 outliers 6 bannermen 1 -----Explanation:-----Explanation: $It$ $is$ $assured$ $that$ $each$ $team$ $will$ $submit$ $once$ $within$ $first$ $half$ $an$ $hour$.That means - that kind of submissions isn't possible within first half an hour. Dataset can be huge. Use faster I/O method. [
763
6RSWYJ30E3EG
On a planet called RUIZ LAND, which is ruled by the queen, Erika Ruiz. Each person on that planet has a strength value (strength value >0). That planet has a special rule made by the queen that a boy and a girl will form a couple if their Hate value is a prime number where $Hate$ is given by the formula:- Hate = (boy's strength value) XOR (girl's strength value ) You are given $N$ numbers denoting the strength value of $N$ girls, and each of the $N$ girls has to form a couple with a boy such that sum of $Hate$ value of all the $N$ couples will be minimum. You need to print the strength value of each boy, Where the boy at index $i$ will form a couple with the girl at index $i$, where $1 \leq i \leq N$. Assume that you can always find at least one boy having that strength for each girl. -----Input:----- - First line will contain $N$, the number of Girls. - Next line contains $N$ numbers separated by space denoting strength value for each girl. -----Output:----- Print the required $N$ numbers denoting strength of boys. -----Constraints----- - $1 \leq N \leq 100000$ - $1 \leq A_i \leq 10^9$ , (where $1 \leq i \leq N$) and $A_i$ denotes strength of i'th girl. -----Sample Input:----- 2 10 16 -----Sample Output:----- 8 18 [
343
5A6N90AVIWK9
Vasya has ordered a pizza delivery. The pizza can be considered a perfect circle. There were $n$ premade cuts in the pizza when it was delivered. Each cut is a straight segment connecting the center of the pizza with its boundary. Let $O$ be the center of the pizza, $P_i$ be the endpoint of the $i$-th cut lying on the boundary, and $R$ be the point of the boundary straight to the right of $O$. Then the counterclockwise-measured angle $\angle ROP_i$ is equal to $a_i$ degrees, where $a_i$ is an integer between $0$ and $359$. Note that angles between $0$ and $180$ angles correspond to $P_i$ in the top half of the pizza, while angles between $180$ and $360$ angles correspond to the bottom half. Vasya may cut his pizza a few more times, and the new cuts still have to be straight segments starting at the center. He wants to make the pizza separated into several equal slices, with each slice being a circular sector with no cuts inside of it. How many new cuts Vasya will have to make? -----Input:----- The first line of input contains $T$ , i.e number of test cases per file. The first line of each test case contains a single integer $n-$ the numbers of premade cuts ($2 \leq n \leq 360$). The second lines contains $n$ integers $a_1, \ldots, a_n-$ angles of the cuts $1, \ldots, n$ respectively ($0 \leq a_1 < \ldots, a_{n - 1} < 360$). -----Output:----- Print a single integer$-$ the smallest number of additional cuts Vasya has to make so that the pizza is divided into several equal slices. -----Constraints----- - $1 \leq T \leq 36$ - $2 \leq n \leq 360$ - $0 \leq a_1 < \ldots, a_{n - 1} < 360$ -----Sample Input:----- 3 4 0 90 180 270 2 90 210 2 0 1 -----Sample Output:----- 0 1 358 -----EXPLANATION:----- In the first sample the pizza is already cut into four equal slices. In the second sample the pizza will be cut into three equal slices after making one extra cut at $330$ degrees. In the third sample Vasya will have to cut his pizza into $360$ pieces of $1$ degree angle each. [
563
MIJF2JGMRMI8
Digory Kirke and Polly Plummer are two kids living next door to each other. The attics of the two houses are connected to each other through a passage. Digory's Uncle Andrew has been secretly doing strange things in the attic of his house, and he always ensures that the room is locked. Being curious, Digory suspects that there is another route into the attic through Polly's house, and being curious as kids always are, they wish to find out what it is that Uncle Andrew is secretly up to. So they start from Polly's house, and walk along the passageway to Digory's. Unfortunately, along the way, they suddenly find that some of the floorboards are missing, and that taking a step forward would have them plummet to their deaths below. Dejected, but determined, they return to Polly's house, and decide to practice long-jumping in the yard before they re-attempt the crossing of the passage. It takes them exactly one day to master long-jumping a certain length. Also, once they have mastered jumping a particular length L, they are able to jump any amount less than equal to L as well. The next day they return to their mission, but somehow find that there is another place further up the passage, that requires them to jump even more than they had practiced for. So they go back and repeat the process. Note the following: - At each point, they are able to sense only how much they need to jump at that point, and have no idea of the further reaches of the passage till they reach there. That is, they are able to only see how far ahead is the next floorboard. - The amount they choose to practice for their jump is exactly the amount they need to get across that particular part of the passage. That is, if they can currently jump upto a length L0, and they require to jump a length L1(> L0) at that point, they will practice jumping length L1 that day. - They start by being able to "jump" a length of 1. Find how many days it will take them to cross the passageway. In the input, the passageway is described as a string P of '#'s and '.'s. A '#' represents a floorboard, while a '.' represents the absence of a floorboard. The string, when read from left to right, describes the passage from Polly's house to Digory's, and not vice-versa. -----Input----- The first line consists of a single integer T, the number of testcases. Each of the next T lines consist of the string P for that case. -----Output----- For each case, output the number of days it takes them to cross the passage. -----Constraints----- - 1 ≤ T ≤ 1,000,000 (106) - 1 ≤ |P| ≤ 1,000,000 (106) - The total length of P will be ≤ 5,000,000 (5 * 106)across all test-cases of a test-file - P will consist of only the characters # and . - The first and the last characters of P will be #. -----Example----- Input: 4 #### ##.#..# ##..#.# ##.#....# Output: 0 2 1 2 -----Explanation----- For the first example, they do not need to learn any jump size. They are able to cross the entire passage by "jumping" lengths 1-1-1. For the second example case, they get stuck at the first '.', and take one day learning to jump length 2. When they come back the next day, they get stuck at '..' and take one day to learn to jump length 3. For the third example case, they get stuck first at '..', and they take one day to learn to jump length 3. On the second day, they are able to jump both length 3 as well as length 2 required to cross the passage. For the last test case they need to stop and learn jumping two times. At first they need to jump a length 2 and then a length 5. -----Appendix----- Irrelevant to the problem description, if you're curious about what Uncle Andrew was up to, he was experimenting on Magic Rings that could facilitate travel between worlds. One such world, as some of you might have heard of, was Narnia. [
923
FVPI6KQY7AIM
Chef has an array A consisting of N integers (1-based indexing). He asks you to perform the following operation M times: for i = 2 to N: Ai = Ai + Ai-1 Your task is to find the xth element of the array (i.e., Ax) after performing the above operation M times. As the answer could be large, please output it modulo 109 + 7. -----Input----- - The first line of input contains an integer T denoting the number of test cases. - The first line of each test case contains three space-separated integers — N, x, and M — denoting the size of the array, index of the element you need to find, and the amount of times you need to repeat operation before finding the element, respectively. The second line contains N space-separated integers A1, A2, …, AN. -----Output----- For each test case, output a single line containing one integer: Ax modulo 109 + 7. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ x ≤ N ≤ 105 - 1 ≤ M ≤ 1018 - 1 ≤ Ai ≤ 1018 -----Subtasks-----Subtask 1 (8 points): - 1 ≤ x ≤ min{2, N}Subtask 2 (24 points): - 1 ≤ N * M ≤ 106Subtask 3 (68 points): No additional constraints -----Example----- Input: 2 3 2 3 1 2 3 3 3 3 1 2 3 Output: 5 15 -----Explanation----- Values in the array A: - Before the operations: [1, 2, 3] - After the first operation: [1, 3, 6] - After the second operation: [1, 4, 10] - After the third operation: [1, 5, 15] Since input file can be fairly large (about 8 MB), it's recommended to use fast I/O (for example, in C++, use scanf/printf instead of cin/cout). [
448
J32K86E0554W
Ganesh lives in Gopalmath. He is looking for Jojo. So he decides to collect Aadhar Card Information of all the citizens of India from UIDAI. Someone told Ganesh that the sum of all the digits of Jojo’s Aadhar number is divisible by 10 and it is greater than zero. After finding all Aadhar numbers which are divisible by 10, Jojo’s Aadhar number is $N$th smallest Aadhar number. Hence, Ganesh wants to find Jojo’s Aadhar number which satisfies all of the above conditions. (In this chaotic world, Aadhar numbers can be any natural number.) However, Guruji refused Ganesh to carry out this task, because he is weak in Maths. Therefore, Ganesh assigns this task to Paritoshbhai who possesses excellent Mathematical skills. Since Paritoshbhai is busy in his jewellery business, help him in this task. -----Input:----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first and only line of each test case contains a single integer N. -----Output:----- For each test case, print a single line containing Aadhar number of Jojo. -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq N \leq 10^{100,000}$ -----Sample Input:----- 1 3 -----Sample Output:----- 37 [
310
VEV4R2UPIS9B
Teddy and Tracy like to play a game based on strings. The game is as follows. Initially, Tracy writes a long random string on a whiteboard. Then, each player starting with Teddy makes turn alternately. Each turn, the player must erase a contiguous substring that exists in the dictionary. The dictionary consists of N words. Of course, the player that can't erase any substring in his turn loses the game, and the other player is declared the winner. Note that after a substring R is erased, the remaining substring becomes separated, i.e. they cannot erase a word that occurs partially to the left of R and partially to the right of R. Determine the winner of the game, assuming that both players play optimally. -----Input----- The first line contains a single integer T, the number of test cases. T test cases follow. The first line of each testcase contains a string S, the string Tracy writes on the whiteboard. The next line contains a single integer N. N lines follow. The i-th line contains a single string wi, the i-th word in the dictionary. -----Output----- For each test case, output a single line containing the name of the winner of the game. -----Example----- Input: 3 codechef 2 code chef foo 1 bar mississippi 4 ssissi mippi mi ppi Output: Tracy Tracy Teddy -----Constraints----- - 1 <= T <= 5 - 1 <= N <= 30 - 1 <= |S| <= 30 - 1 <= |wi| <= 30 - S and wi contain only characters 'a'-'z' [
352
K9PXJFSKCK4J
Mathison recently inherited an ancient papyrus that contained some text. Unfortunately, the text was not a pangram. Now, Mathison has a particular liking for holoalphabetic strings and the text bothers him. The good news is that Mathison can buy letters from the local store in order to turn his text into a pangram. However, each letter has a price and Mathison is not very rich. Can you help Mathison find the cheapest way to obtain a pangram? -----Input----- The first line of the input file will contain one integer, T, representing the number of tests. Each test will be formed from two lines. The first one contains 26 space-separated integers, representing the prices of all letters. The second will contain Mathison's initial text (a string of N lowercase letters). -----Output----- The output file will contain T lines, one for each test. Each line will contain the answer for the corresponding test. -----Constraints and notes----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 50,000 - All prices are natural numbers between 1 and 1,000,000 (i.e. 106). - A pangram is a string that contains every letter of the Latin alphabet at least once. - All purchased letters are added to the end of the string. -----Subtaks----- Subtask #1 (30 points): - N = 1 Subtask #2 (70 points): - Original constraints -----Example----- Input: 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 abcdefghijklmopqrstuvwz 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 thequickbrownfoxjumpsoverthelazydog Output: 63 0 -----Explanation----- First test There are three letters missing from the original string: n (price 14), x (price 24), and y (price 25). Therefore the answer is 14 + 24 + 25 = 63. Second test No letter is missing so there is no point in buying something. The answer is 0. [
514
8KS52R5A1DIU
You are given a sequence of integers $A_1,A_2,…,A_N$ and a magical non-zero integer $x$ You have to select a subsegment of sequence A (possibly empty), and replace the elements in that subsegment after dividing them by x. Formally, replace any one subsegment $A_l, A_{l+1}, ..., A_r$ with $A_l/x, A_{l+1}/x, ..., A_r/x$ where $l \leq r$ What is the minimum possible sum you can obtain? Note: The given operation can only be performed once -----Input ----- - The first line of the input contains two positive integer n denoting the size of array, and x denoting the magical integer - Next line contains $N$ space separated integers -----Output----- Single line containing one real number, denoting the minimum possible sum you can obtain. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-2}$ -----Constraints----- - $1 \leq n \leq 10^3$ - $1 \leq |x| \leq 10^3$ - $ |A_i| \leq 10^3$ -----Sample Input----- 3 2 1 -2 3 -----Sample Output----- 0.5 -----Explanation----- Array 1 -2 3, selecting subsegment {3}, you get 1 -2 1.5, which gives $sum=0.5$ [
318
LV3AMBK6VOUY
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b"). [
317
ZN7DYFFDHCCC
Chef is playing a game on the non-negative x-axis. It takes him $1$ second to reach from $i^{th}$ position to $(i-1)^{th}$ position or $(i+1)^{th}$ position. The chef never goes to the negative x-axis. Also, Chef doesn't stop at any moment of time. The movement of chef can be described as follows. - At the start he is standing at $x=0$ at time $0$. - In the first round, he moves towards $x=1$ and comes back to the $x=0$ position. - In the second round, he moves towards the $x=2$ and comes back again to $x=0$. - Generalizing, in the $k^{th}$ round, he moves from $x=0$ to $x=k$ and then returns back to $x=0$ at the end of the round. This goes on as the game progresses. For Example, the path of Chef for $3^{rd}$ round is given below. $0 - 1 - 2 - 3 - 2 - 1 - 0$ The overall path followed by Chef would look somewhat like this: $0 - 1 - 0 - 1 - 2 - 1 - 0 - 1 - 2 - 3 - 2 - 1 - 0 - 1 - 2 - 3 - 4 - 3 - …$ You are given two non-negative integers $N$ and $K$. You have to tell the time at which Chef arrives at $x=N$ for the $K^{th}$ time. Note - Chef can not skip a position while visiting the positions. -----Input:----- - The first line contains $T$ the number of test cases. Then the test cases follow. - Each test case contains a single line of two integers $N$ and $K$. -----Output:----- For each test case, print a single line containing one integer -- the time taken by the chef to arrive at $x=N$ for the $K^{th}$ time by modulo $1,000,000,007$. -----Constraints----- - $1 \le T \le 10^5$ - $0 \le N \le 10^9$ - $1 \le K \le 10^9$ -----Sample Input:----- 5 0 1 1 1 2 1 1 3 4 6 -----Sample Output:----- 0 1 4 5 46 -----Explanation:----- Test Case 1: Chef starts the journey from the $N = 0$ at time $t = 0$ and it's the first time $(K = 1)$, he is here. So, the answer is $0$. Test Case 2: Chef starts the journey from the $N = 0$ at time $t = 0$ then goes to $N = 1$ at $t = 1$ and it's the first time $(K = 1)$, he is here. So, the answer is $1$. Test Case 4: The path followed by Chef to reach $1$ for the third time is given below. $0 - 1 - 0 - 1 - 2 - 1$ He reaches $1$ for the third time at $t=5$. [
720
UXSOHF6WBPID
Chef Ada is building a new restaurant in the following way: - First, $N$ points $X_1, X_2, \ldots, X_N$ are chosen on the $x$-axis. - Then, $N$ columns (numbered $1$ through $N$) are made. For simplicity, the columns are represented as vertical segments; for each valid $i$, the height of the $i$-th segment is $H_i$. - Ada assigns a column to each of the points $X_1, X_2, \ldots, X_N$ in an arbitrary way (each column must be assigned to exactly one point). - Finally, Ada constructs the roof of the restaurant, represented by a polyline with $N$ vertices. Let's denote the column assigned to the $i$-th point by $P_i$. For each valid $i$, the $i$-th of these vertices is $(X_i, H_{P_i})$, i.e. the polyline joins the tops of the columns from left to right. Ada wants the biggest restaurant. Help her choose the positions of the columns in such a way that the area below the roof is the biggest possible. Formally, she wants to maximise the area of the polygon whose perimeter is formed by the roof and the segments $(X_N, H_{P_N}) - (X_N, 0) - (X_1, 0) - (X_1, H_{P_1})$. Let $S$ be this maximum area; you should compute $2 \cdot S$ (it is guaranteed that $2 \cdot S$ is an integer). -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $X_i$ and $H_i$. -----Output----- For each test case, print a single line containing one integer $2 \cdot S$. -----Constraints----- - $1 \le T \le 3 \cdot 10^5$ - $2 \le N \le 10^5$ - $0 \le X_1 < X_2 < \ldots < X_N \le 2 \cdot 10^9$ - $1 \le H_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^6$ -----Example Input----- 1 5 1 1 2 2 3 3 4 4 5 5 -----Example Output----- 27 -----Explanation----- [
596
JF53AGXJ8893
Roman has no idea, why this problem is called Stone. He also has no idea on how to solve the followong problem: given array of N integers A and a number K. During a turn the maximal value over all Ai is chosen, let's call it MAX. Then Ai = MAX - Ai is done for every 1 <= i <= N. Help Roman to find out how will the array look like after K turns. -----Input----- The numbers N and K are given in the first line of an input. Then N integers are given in the second line which denote the array A. -----Output----- Output N numbers on a single line. It should be the array A after K turns. -----Constraints----- - 1 <= N <= 105 - 0 <= K <= 109 - Ai does not exceed 2 * 109 by it's absolute value. -----Example----- Input: 4 1 5 -1 7 0 Output: 2 8 0 7 [
207
T338QGY3255D
Chef just got a box of chocolates as his birthday gift. The box contains $N$ chocolates in a row (numbered $1$ through $N$), where $N$ is even. For each valid $i$, the $i$-th chocolate has a sweetness value $W_i$. Chef wants to eat all the chocolates in the first half of the box and leave all chocolates in the second half uneaten. Since he does not like chocolates that are too sweet, he will be unhappy if at least one of the chocolates he eats has the maximum sweetness among all the chocolates in the box. A right cyclic shift by $k$ chocolates ($0 \le k < N$) consists of moving the last $k$ chocolates in the row to the beginning in the same order and moving each of the remaining $N-k$ chocolates $k$ places to the right. Before eating the first half of the chocolates, Chef wants to perform some right cyclic shift in such a way that he will not be unhappy after eating them. Find the number of ways to do this, i.e. the number of valid integers $k$ such that if Chef performs the right cyclic shift by $k$ chocolates and then eats the first half of the chocolates in the box, he does not become unhappy. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N$ space-separated integers $W_1, W_2, \ldots, W_N$. -----Output----- For each test case, print a single line containing one integer ― the number of shifts for which Chef does not become unhappy. -----Constraints----- - $1 \le T \le 5$ - $1 \le N \le 10^5$ - $N$ is even - $1 \le W_i \le 10^5$ for each valid $i$ -----Example Input----- 2 6 1 1 2 1 1 1 6 1 1 2 1 1 2 -----Example Output----- 3 0 -----Explanation----- Example case 1: The three valid right shifts and the contents of the box for these shifts are: - shift by $k = 1$: $(1, 1, 1, 2, 1, 1)$ - shift by $k = 2$: $(1, 1, 1, 1, 2, 1)$ - shift by $k = 3$: $(1, 1, 1, 1, 1, 2)$ [
574