Let's call an array a consisting of n positive (greater than 0 ) integers beautiful if the following condition is held for every i from 1 to n : either ai=1 , or at least one of the numbers ai−1 and ai−2 exists in the array as well.
For example:
You are given a positive integer s . Find the minimum possible size of a beautiful array with the sum of elements equal to s .
The first line contains one integer t (1≤t≤5000 ) — the number of test cases.
Then t lines follow, the i -th line contains one integer s (1≤s≤5000 ) for the i -th test case.
Print t integers, the i -th integer should be the answer for the i -th testcase: the minimum possible size of a beautiful array with the sum of elements equal to s .
4 1 8 7 42
1 3 3 7
Consider the example test:
You are given a string s of length n consisting only of the characters 0 and 1.
You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length l , you get a⋅l+b points.
Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.
The first line contains a single integer t (1≤t≤2000 ) — the number of testcases.
The first line of each testcase contains three integers n , a and b (1≤n≤100;−100≤a,b≤100 ) — the length of the string s and the parameters a and b .
The second line contains the string s . The string s consists only of the characters 0 and 1.
For each testcase, print a single integer — the maximum number of points that you can score.
3 3 2 0 000 5 -2 5 11001 6 1 -4 100111
6 15 -2
In the first example, it is enough to delete the entire string, then we will get 2⋅3+0=6 points.
In the second example, if we delete characters one by one, then for each deleted character we will get (−2)⋅1+5=3 points, i. e. 15 points in total.
In the third example, we can delete the substring 00 from the string 100111, we get 1⋅2+(−4)=−2 points, and the string will be equal to 1111, removing it entirely we get 1⋅4+(−4)=0 points. In total, we got −2 points for 2 operations.
Suppose you have two points p=(xp,yp) and q=(xq,yq) . Let's denote the Manhattan distance between them as d(p,q)=|xp−xq|+|yp−yq| .
Let's say that three points p , q , r form a bad triple if d(p,r)=d(p,q)+d(q,r) .
Let's say that an array b1,b2,…,bm is good if it is impossible to choose three distinct indices i , j , k such that the points (bi,i) , (bj,j) and (bk,k) form a bad triple.
You are given an array a1,a2,…,an . Calculate the number of good subarrays of a . A subarray of the array a is the array al,al+1,…,ar for some 1≤l≤r≤n .
Note that, according to the definition, subarrays of length 1 and 2 are good.
The first line contains one integer t (1≤t≤5000 ) — the number of test cases.
The first line of each test case contains one integer n (1≤n≤2⋅105 ) — the length of array a .
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109 ).
It's guaranteed that the sum of n doesn't exceed 2⋅105 .
For each test case, print the number of good subarrays of array a .
3 4 2 4 1 3 5 6 9 1 9 6 2 13 37
10 12 3
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a2,a3,a4] is good since it contains only three elements and:
In the second test case, for example, subarray [a1,a2,a3,a4] is not good, since it contains a bad triple (a1,1) , (a2,2) , (a4,4) :
So, d((a1,1),(a4,4))=d((a1,1),(a2,2))+d((a2,2),(a4,4)) .
Let's call an integer array a1,a2,…,an good if ai≠i for each i .
Let F(a) be the number of pairs (i,j) (1≤i<j≤n ) such that ai+aj=i+j .
Let's say that an array a1,a2,…,an is excellent if:
Given n , l and r , calculate the number of excellent arrays modulo 109+7 .
The first line contains a single integer t (1≤t≤1000 ) — the number of test cases.
The first and only line of each test case contains three integers n , l , and r (2≤n≤2⋅105 ; −109≤l≤1 ; n≤r≤109 ).
It's guaranteed that the sum of n doesn't exceed 2⋅105 .
For each test case, print the number of excellent arrays modulo 109+7 .
4 3 0 3 4 -3 5 42 -33 55 69 -42 146
4 10 143922563 698570404
In the first test case, it can be proven that the maximum F(a) among all good arrays a is equal to 2 . The excellent arrays are:
You are given a string s of length n . Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let fi be the maximum length substring of string s , which consists entirely of the i -th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i -th letter doesn't appear in a string, then fi is equal to 0 .
The value of a string s is the minimum value among fi for all i from 1 to k .
What is the maximum value the string can have?
The first line contains two integers n and k (1≤n≤2⋅105 ; 1≤k≤17 ) — the length of the string and the number of first Latin letters used.
The second line contains a string s , consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Print a single integer — the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
10 2 a??ab????b
4
9 4 ?????????
2
2 3 ??
0
15 3 ??b?babbc??b?aa
3
4 4 cabd
1
In the first example the question marks can be replaced in the following way: "aaaababbbb". f1=4 , f2=4 , thus the answer is 4 . Replacing it like this is also possible: "aaaabbbbbb". That way f1=4 , f2=6 , however, the minimum of them is still 4 .
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values fi is always 0 .
There is an infinite pond that can be represented with a number line. There are n rocks in the pond, numbered from 1 to n . The i -th rock is located at an integer coordinate ai . The coordinates of the rocks are pairwise distinct. The rocks are numbered in the increasing order of the coordinate, so a1<a2<⋯<an .
A robot frog sits on the rock number s . The frog is programmable. It has a base jumping distance parameter d . There also is a setting for the jumping distance range. If the jumping distance range is set to some integer k , then the frog can jump from some rock to any rock at a distance from d−k to d+k inclusive in any direction. The distance between two rocks is an absolute difference between their coordinates.
You are assigned a task to implement a feature for the frog. Given two integers i and k determine if the frog can reach a rock number i from a rock number s performing a sequence of jumps with the jumping distance range set to k . The sequence can be arbitrarily long or empty.
You will be given q testcases for that feature, the j -th testcase consists of two integers i and k . Print "Yes" if the i -th rock is reachable and "No" otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and 'YES"' will be recognized as a positive answer).
The first line contains four integers n , q , s and d (1≤n,q≤2⋅105 ; 1≤s≤n ; 1≤d≤106 ) — the number of rocks, the number of testcases, the starting rock and the base jumping distance parameter.
The second line contains n integers a1,a2,…,an (1≤ai≤106 ) — the coordinates of the rocks. The coordinates of the rocks are pairwise distinct. The rocks are numbered in the increasing order of distance from the land, so a1<a2<⋯<an .
Each of the next q lines contains two integers i and k (1≤i≤n ; 1≤k≤106 ) — the parameters to the testcase.
For each of the testcases print an answer. If there is a sequence of jumps from a rock number s to a rock number i with the jumping distance range set to k , then print "Yes". Otherwise, print "No".
7 4 4 5 1 5 10 13 20 22 28 4 1 7 2 7 3 3 2
Yes No Yes Yes
10 8 6 11 1 2 4 7 8 9 11 13 19 20 2 13 5 8 8 1 6 15 1 15 2 7 7 6 8 9
Yes Yes No Yes Yes Yes Yes Yes
6 9 6 6 1 2 4 9 18 19 2 17 1 18 5 4 2 11 5 17 6 8 4 3 3 3 6 6
Yes Yes Yes Yes Yes Yes No No Yes
4 1 1 10 1 8 10 19 2 1
Yes
Explanation of the first example:
In the first testcase the destination rock is the same as the starting rock, thus no jumps are required to reach it.
In the second testcase the frog can jump any distance in the range [5−2;5+2] . Thus, it can reach rock number 5 (by jumping 7 to the right) and rock number 3 (by jumping 3 to the left). From rock number 3 it can reach rock number 2 (by jumping 5 to the left). From rock number 2 it can reach rock number 1 (by jumping 4 to the left). However, there is no way to reach rock number 7 .
In the third testcase the frog can jump any distance in the range [5−3;5+3] . Thus, it can reach rock number 7 by jumping to rock 5 first and to 7 afterwards.
The fourth testcase is shown in the explanation for the second testcase.