Bear Limak has a sequence of N non-negative integers A1, A2, ..., AN. He defines the score of a segment (consecutive subsequence) as its sum of elements modulo P (not necessarily prime). Find the maximum score of a non-empty segment, and also find the number of segments with this maximum score.

-----Input-----
First line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
For each test case, the first line of the input contains two space separated integers, N and P.
The second line contains N space separated integers denoting the sequence.

-----Output-----
For each test case, output two space separated integers denoting the maximum score of a segment and the number of segments with the score, respectively.

-----Constraints-----
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ P ≤ 109
- 0 ≤ Ai ≤ 109 

Subtask #1: (25 points)
- 1 ≤ N ≤ 100

Subtask #2: (25 points)
- 1 ≤ N ≤ 1000

Subtask #3: (50 points)
- original constraints

-----Example-----
Input:
4
2 3
1 2
3 5
2 4 3
3 100
1 3 5
4 3
1 2 3 4

Output:
2 1
4 2
9 1
2 2

-----Explanation-----
Example case 1. There are three segments - [1], [2] and [1, 2]. Sum of these segments are 1, 2 and 3 respectively. Sum of these segments modulo 3 will be 1, 2 and 0. Maximum score among these is 2. There is also only one segment with this score.
Example case 2. There are six segments - [2], [4], [3], [2, 4], [4, 3] and [2, 4, 3]. Sum of these segments are 2, 4, 3, 6, 7, 9 respectively. Sum of these segments modulo 5 will be 2, 4, 3, 1, 2, 4. Maximum score among these is 4. And there are two segments with this score.

Source: codechef | Difficulty: HARD
Tags: Mathematics

Data structures

Range queries


Solutions:
t = int(input())
for _ in range(t):
	(n, p) = map(int, input().split())
	l = list(map(int, input().split()))
	d = {}
	for i in range(n):
		c = 0
		for j in range(i, n):
			c += l[j]
			try:
				d[c % p] += 1
			except:
				d[c % p] = 1
	x = max(list(d.keys()))
	print(x, d[x])


from bisect import bisect_left, bisect_right
T = int(input())
for t in range(T):
	(n, p) = map(int, input().split())
	data = map(int, input().split())
	temp = [0]
	for v in data:
		temp.append((temp[-1] + v) % p)
	record = 0
	count = 0
	arr = []
	for v in temp:
		i = bisect_right(arr, v)
		if i < len(arr):
			u = arr[i]
			vv = (v - u) % p
		else:
			u = 0
			vv = v
		delta = bisect_right(arr, u) - bisect_left(arr, u)
		if record < vv:
			record = vv
			count = delta
		elif record == vv:
			count += delta
		arr.insert(i, v)
	print(record, count)


for _ in range(int(input())):
	(n, p) = map(int, input().split())
	l = [int(i) for i in input().split()]
	pre = [0] * (n + 1)
	for i in range(1, n + 1):
		pre[i] = pre[i - 1] + l[i - 1]
		pre[i] %= p
	cmaxi = 0
	maxi = 0
	for i in range(n):
		for j in range(i, n):
			curr = (pre[j + 1] - pre[i] + p) % p
			if curr > maxi:
				maxi = curr
				cmaxi = 1
			elif curr == maxi:
				cmaxi += 1
	print(maxi, cmaxi)


for _ in range(int(input())):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	x = y = c = 0
	l = list(map(int, input().split()))
	for i in range(n):
		for j in range(i, n):
			x = x + l[j]
			if x % m > y:
				y = x % m
				c = 1
			elif y == x % m:
				c += 1
		x = 0
	print(y, c)