You are given a sequence of $$$n$$$ distinct favourite integers $$$a_1,a_2,a_3,\ldots,a_n$$$ and a number $$$x$$$.
Your task is to calculate the sum of all integers from $$$1$$$ to $$$x$$$, but you should take favourite integers with minus in the sum.
For example, $$$a = [1,4,6], x = 5$$$ the sum is equal to $$$- 1 + 2 + 3 - 4 + 5 = 5$$$, because $$$1$$$, $$$4$$$ are favourite integers. We don't consider $$$6$$$ in our sum as we have to take sum from $$$1$$$ to $$$x$$$ only.
The first line contains a single integer $$$t (1 \le t \le 10^5)$$$ — the number of test cases in the input. Then $$$t$$$ test cases follow.
Each query contains two lines. The first line contains two integer $$$n (1 \le n \le 10^5)$$$: the number of favourite integers in the sequence and $$$ x (1 \le x \le 10^9 )$$$, and the second line contains $$$n$$$ distinct favourite integers $$$a_1, \ldots ,a_n (1\le a_i \le 10^9)$$$.
It is guaranteed that the total sum of $$$n$$$ is at most $$$10^5$$$ and all favourite integers are distinct.
Print the requested sum for each of $$$t$$$ test case.
3 3 5 1 4 6 3 3 2 3 4 3 1000000000 1 2 3
5 -4 500000000499999988
The answer for the first sample is explained in the statement.
The sum of second query is — $$$ +1 -2 - 3 = -4$$$.
We have a sequence of $$$n$$$ integers and a number $$$x$$$.
Initially $$$a_1 = a_2 = a_3 = \ldots = a_n = x$$$.
In one move, you can choose any index $$$i$$$ if and only if $$$a_i = a_{i+1}$$$ and $$$1 \le i \le n-1$$$ and perform a operation : $$$ a_i = a_i + a_{i+1} $$$ and $$$a_{i+1} = x$$$.
For example - $$$n = 3, x = 5$$$, Initially sequence is $$$a = [5,5,5]$$$,
choose $$$i = 1$$$, array will change to $$$a = [10,5,5]$$$.
choose $$$i = 2$$$, array will change to $$$a = [10,10,5]$$$.
choose $$$i = 1$$$, array will change to $$$a = [20,5,5]$$$.
choose $$$i = 2$$$, array will change to $$$a = [20,10,5]$$$.You cannot choose any index now.
You can perform any number of moves, and any index can be chosen any number of times (including zero). Your task is to find out the maximum possible sum of array we can obtain.
As the answer can be rather large, print the remainder after dividing it by $$$1000000007 (10^9 + 7)$$$.
The first line contains a single integer $$$t (1 \le t \le 10^5)$$$ — the number of test cases in the input. Then $$$t$$$ test cases follow.
Each query contains two integers - $$$n (1\le n \le 10^9)$$$ : total numbers in the sequence and $$$x (1\le x \le 10^9) $$$.
For each test from the input print the maximum possible sum we can obtain by performing moves any number of time modulo $$$1000000007 (10^9 + 7)$$$.
3 1 4 2 3 3 5
4 9 35
In the first query — Initially $$$a = [4]$$$. we cannot perform any moves.
In the second query — Initially $$$a = [3,3]$$$.
choose $$$i = 1$$$, array will change to $$$a = [6,3]$$$. now we cannot perform any move. So maximum sum is $$$9$$$.
Last query is discussed in Problem Statement.
For the multiset of positive integers $$$a = {(a_1,a_2, \ldots ,a_k)}$$$ define the Greatest Common Divisor (GCD) of $$$a$$$ as:
$$$gcd(a)$$$ is the maximum positive integer $$$x$$$, such that all integers in multiset $$$a$$$ are divisible on $$$x$$$.
For example, $$$gcd({8,12,16})=4$$$.
Singhal has a sequence consisting of $$$n$$$ integers. He wants to find the subarray of $$$(length \gt 1)$$$ whose $$$gcd$$$ is maximum possible. If there exist multiple subarrays then choose the largest in length. Help him to find the maximum gcd he can obtain and length of this subarray.
A subarray is the sequence of consecutive elements of the array.
The first line contains a single integer $$$t (1 \le t \le 10^5)$$$ — the number of test cases in the input. Then $$$t$$$ test cases follow.
Each query contains two lines. The first line contains one integer $$$n (2 \le n \le 10^5)$$$: the number of integers in the sequence, and the second line contains $$$n$$$ integers $$$a_1, \ldots ,a_n (1\le a_i \le 10^9)$$$.
It is guaranteed that the total sum of $$$n$$$ is at most $$$10^5$$$.
Print $$$t$$$ answers to the test cases. Each answer must be consisting of two integers separate by a space — the maximum gcd he can obtain and the length of the maximum subarray.
4 3 3 6 9 3 3 6 4 2 4 8 3 4 4 4
3 3 3 2 4 2 4 3
In the first query, possible subarray with $$$(length \gt 1)$$$ are :
$$$gcd({3,6}) = 3 \,, gcd({6,9}) = 3 \,, gcd({3,6,9}) = 3. $$$
All the subarray has same $$$gcd = 3$$$, we have to pick the largest length i.e $$$3$$$.
In the second query, possible subarray with $$$(length \gt 1)$$$ are :
$$$gcd({3,6}) = 3 \,, gcd({6,4}) = 2 \,, gcd({3,6,4}) = 1. $$$
Maximum $$$gcd $$$ we can obtain is $$$3$$$, and the largest length is $$$2$$$.
Singhal has a array of integers $$$a_1,a_2,\ldots,a_n$$$.
Definition of an awesome array —
If $$$K$$$ is the maximum element in the array, then the elements before $$$K$$$ in the array should be in the strictly ascending order and the elements after $$$K$$$ in the array should be in the strictly descending order.
Examples of awesome array are — $$$(1,2,3,1) , (1,2,3) , (3,2,1) , (1,2,1), (2,3,2,1) $$$ , but not $$$ (1,2,2) , (2,2,1) , (1,2,2,3) , (1,3,2,2). $$$
He wants to know that how many distinct awesome arrays he will get after reordering the sequence.
Reorder of array $$$ (1,3,3,4) $$$ can results $$$(1,4,3,3) , (3,4,3,1) ,(4,3,1,3) $$$, but not $$$ (1,3,3,3) , (1,3,3) , (1,3,3,4,4) $$$. You are not allowed to change the value, insert or delete.
Notice that the answer might be large, please output the desired value modulo $$$10^9+7$$$.
The first line contains a single integer $$$t (1 \le t \le 10^5)$$$ — the number of test cases in the input. Then $$$t$$$ test cases follow.
Each query contains two lines. The first line contains one integer $$$n (1 \le n \le 10^5)$$$: the number of integers in the sequence, and the second line contains $$$n$$$ integers $$$a_1, \ldots ,a_n (1\le a_i \le 10^9)$$$.
It is guaranteed that the total sum of $$$n$$$ is at most $$$10^5$$$.
Print $$$t$$$ answers to the test cases. Each answer must be consisting of single integer — the number of awesome arrays modulo $$$10^9+7$$$
4 3 1 1 2 3 1 2 3 2 1 2 3 2 1 2
1 4 2 0
In the first query,
There are a total of 3 possible array after reordering the given array $$$(1, 1, 2)$$$. They are: $$$(1,1,2)$$$, $$$(1,2,1)$$$ and $$$(2,1,1)$$$.
Out of the above, only $$$(1, 2, 1)$$$ is the awesome array.
In the second query,
There are a total of $$$6$$$ possible array after reordering. They are: $$$({1, 2, 3}), ({1, 3, 2}), ({2, 1, 3}), ({2, 3, 1}), ({3, 1, 2})$$$ and $$$ ({3, 2, 1})$$$.
Out of the above $$$4$$$ are awesome arrays.
In the last query ,
No awesome array we can formed after reordering the given array.
Yestarday, Singhal went to a shop. The shop owner is a mathematician and has a unique style of costing the items costumer bought.
If a item has a initial price $$$X (X \ge 2)$$$ , then costumer can bought $$$N$$$ items of this price if $$$X\pmod N = 0$$$ and $$$ 1 \le N \le X-1$$$.
Example - If a item cost $$$X = 10$$$, then costumer can buy these $$$(1,2,5)$$$ numbers of items of this price.
If a item cost $$$X = 12$$$, then costumer can buy these $$$(1,2,3,4,6)$$$ numbers of items.
The shop owner will then calculates the total cost of $$$N$$$ items as -
1) if $$$N$$$ is Prime , cost will be $$$$$$ A + \frac{X}{N} $$$$$$
2) if $$$N$$$ is Composite , cost will be $$$$$$ B + \frac{X}{N} $$$$$$
3) if $$$N$$$ is $$$1$$$ , cost will be $$$$$$ C + \frac{X}{N} $$$$$$
Defn of Prime and Composite number - https://www.mathsisfun.com/prime-composite-number.html.
You know the value of $$$A,B,C$$$ and $$$X$$$. Help him to buy $$$N$$$ number of items of price $$$X$$$ so that cost will be minimum possible.
You don't have to minimize the number of items , only minimize the total cost.
The first line contains a single integer $$$t (1 \le t \le 10^5)$$$ — the number of test cases in the input. Then $$$t$$$ test cases follow.
Each query contains two lines. The first line contains a integer $$$X(2 \le X \le 10^7)$$$: Price of item , and the second line contains $$$A,B,C (1\le A,B,C \le 10^9)$$$.
Print $$$t$$$ answers to the test cases. Each answer must be consisting of single integer — the minimum possible cost.
3 12 2 5 1 12 2 3 1 12 15 15 1
6 5 13
In the first query, possible numbers of items he can buy is $$$(1,2,3,4,6)$$$.
Cost for each N is —
$$$ N = 1 - C + \frac{X}{N}$$$ $$$i.e.$$$ $$$1 + \frac{12}{1} = 13$$$
$$$ N = 2 - 2 + \frac{12}{2} = 8$$$
$$$ N = 3 - 2 + \frac{12}{3} = 6$$$
$$$ N = 4 - 5 + \frac{12}{4} = 8$$$
$$$ N = 6 - 5 + \frac{12}{6} = 7$$$
Minimum cost from this will be - $$$6$$$.
In the second query, possible numbers of items he can buy is $$$(1,2,3,4,6)$$$ and Cost for each N will be $$$(13,8,6,6,5)$$$ respectively.
In the third query, possible numbers of items he can buy is $$$(1,2,3,4,6)$$$ and Cost for each N will be $$$(13,21,19,18,17)$$$ respectively.
Given a sequence of integers $$$a_1 , a_2 , \ldots , a_n$$$ and a prime number $$$x$$$.
We have a function which takes $$$length$$$ as a parameter and return the maximum possible value of $$$ (a_i\times a_{i+1}\times \cdots \times a_{i+length-1})\pmod x$$$ , where $$$1 \le i \le (n-length+1) $$$.
For example – $$$a = [2,3,4,2] , x = 5 $$$.
For $$$length = 1$$$ , function return $$$\max(2,3,4,2) = 4$$$.
For $$$length = 2$$$ , function return $$$\max((2\times 3)\pmod 5 , (3\times 4)\pmod 5 , (4\times 2)\pmod 5) = \max(1,2,3) = 3$$$.
For $$$length = 3$$$ , function return $$$\max((2\times 3\times 4)\pmod 5 , (3\times 4\times 2)\pmod 5) = \max(4,4) = 4$$$.
For $$$length = 4$$$ , function return $$$\max((2\times 3\times 4\times 2)\pmod 5) = \max(3) = 3$$$.
Your task is to find the smallest length for which this function return maximum possible value.
The first line contains a single integer $$$t (1 \le t \le 10^5)$$$ — the number of test cases in the input. Then $$$t$$$ test cases follow.
Each query contains two lines. The first line contains two integer $$$n (1 \le n \le 10^5)$$$: the number of integers in the sequence and $$$x (2 \le x \le 97)$$$, and the second line contains $$$n$$$ integers $$$a_1, \ldots ,a_n (1\le a_i \lt x)$$$.
It is guaranteed that the total sum of $$$n$$$ is at most $$$10^5$$$ and $$$x$$$ be a prime number.
Print $$$t$$$ answers to the test cases. Each answer must be consisting of two integers — the maximum possible value of function and the smallest length for which function return maximum possible value.
5 4 5 2 3 4 2 4 3 1 1 1 1 3 7 2 3 6 3 7 5 5 5 3 11 5 6 7
4 1 1 1 6 1 6 3 9 2
In the first query — Explained in Problem Statement. For $$$length = 1$$$ and $$$3$$$, Function return maximum value i.e $$$4$$$. We have to return the minimum length i.e $$$1$$$.
In the fourth query —
For $$$length = 1$$$ , function return $$$\max(5,5,5) = 5$$$.
For $$$length = 2$$$ , function return $$$\max((5\times 5)\pmod 7 , (5\times 5)\pmod 7) = \max(4,4) = 4$$$.
For $$$length = 3$$$ , function return $$$\max((5\times 5\times 5)\pmod 7) = \max(6) = 6$$$.
Singhal has a sequence of integers $$$a_1 , a_2 , \ldots , a_n$$$.
Recall,
A subarray is the sequence of consecutive elements of the array.
$$$ a\pmod n$$$ is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.
A subarray $$$a_i , a_{i+1} , \ldots , a_j $$$ is good if
$$$$$$ (a_i*a_{i+1}*\cdots*a_j)\pmod n = 1 $$$$$$
Formally,
$$$$$$ \Bigl( \prod_{k=i}^j a_k \Bigr)\pmod n = 1$$$$$$
Help him to find if a good subarray is present in his sequence or not. If there are many good subarray tell the smallest possible in length.
The first line contains a single integer $$$t (1 \le t \le 10^5)$$$ — the number of test cases in the input. Then $$$t$$$ test cases follow.
Each query contains two lines. The first line contains one integer $$$n (1 \le n \le 10^5)$$$: the number of integers in the sequence, and the second line contains $$$n$$$ integers $$$a_1, \ldots ,a_n (1\le a_i \le 10^9)$$$.
It is guaranteed that the total sum of $$$n$$$ is at most $$$10^5$$$.
Print $$$t$$$ answers to the test cases. Each answer must be consisting of single integer — the smallest possible length of good subarray if present in the sequence , if not output $$$0$$$.
4 3 2 2 6 4 2 2 3 3 2 2 2 3 2 1 2
2 2 0 1
In the first query, possible subarray are —
$$$ (2) - 2\pmod 3 = 2 $$$
$$$ (2) - 2\pmod 3 = 2 $$$
$$$ (6) - 6\pmod 3 = 0 $$$
$$$ (2,2) - (2*2)\pmod 3 = 1 $$$
$$$ (2,6) - (2*6)\pmod 3 = 0 $$$
$$$ (2,2,6) - (2*2*6)\pmod 3 = 0 $$$
Only one good subarray $$$(2,2)$$$ , length - $$$2$$$.
In the second query , only one good subarray $$$(3,3) - (3*3)\pmod 4 = 1$$$ , length - $$$2$$$.
In third query , no good subarray possible.
In fourth query , two good subarray possible —
$$$(1) - 1\pmod 3 = 1 $$$
$$$(2,1,2) - (2*1*2)\pmod 3 = 1 $$$
We have to choose one with smallest length , i.e. $$$1$$$.