Sherlock and Watson have recently enrolled in a computer programming course. Today, the tutor taught them about the balanced parentheses problem. A string S
consisting only of characters (
and/or )
is balanced if:
(
S)
, where S is a balanced string, or:(
and a total of R right parentheses )
. Moreover, the string must have as many different balanced non-empty substrings as possible. (Two substrings are considered different as long as they start and end at different indexes of the string, even if their content happens to be the same). Note that S itself does not have to be balanced.The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line with two integers: L and R.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the answer, as described above.
1 ≤ T ≤ 100.
Time limit: 20 seconds per test set.
Memory limit: 1GB.
0 ≤ L ≤ 20.
0 ≤ R ≤ 20.
1 ≤ L + R ≤ 20.
0 ≤ L ≤ 105.
0 ≤ R ≤ 105.
1 ≤ L + R ≤ 105.
3 1 0 1 1 3 2
Case #1: 0 Case #2: 1 Case #3: 3
(
. There are no balanced non-empty substrings.()
. There is only one balanced non-empty substring: the entire string itself.()()(
and (()()
give the same optimal answer.()()(
, for example, the three balanced substrings are ()
from indexes 1 to 2, ()
from indexes 3 to 4, and ()()
from indexes 1 to 4.
Watson and Sherlock are gym buddies.
Their gym trainer has given them three numbers, A, B, and N, and has asked Watson and Sherlock to pick two different positive integers i and j, where i and j are both less than or equal to N. Watson is expected to eat exactly iA sprouts every day, and Sherlock is expected to eat exactly jB sprouts every day.
Watson and Sherlock have noticed that if the total number of sprouts eaten by them on a given day is divisible by a certain integer K, then they get along well that day.
So, Watson and Sherlock need your help to determine how many such pairs of (i, j) exist, where i != j. As the number of pairs can be really high, please output it modulo 109+7 (1000000007).
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line with 4 integers A, B, N and K, as described above.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the required answer.
1 ≤ T ≤ 100.
Time limit: 60 seconds per test set.
Memory limit: 1GB.
0 ≤ A ≤ 106.
0 ≤ B ≤ 106.
1 ≤ K ≤ 10000.
1 ≤ N ≤ 1000.
1 ≤ K ≤ 100000.
1 ≤ N ≤ 1018.
3 1 1 5 3 1 2 4 5 1 1 2 2
Case #1: 8 Case #2: 3 Case #3: 0
Sherlock and Watson have mastered the intricacies of the language C++ in their programming course, so they have moved on to algorithmic problems. In today's class, the tutor introduced the problem of merging one-dimensional intervals. N intervals are given, and the i
th interval is defined by the inclusive endpoints [Li, Ri], where Li ≤ Ri.
The tutor defined the covered area of a set of intervals as the number of integers appearing in at least one of the intervals. (Formally, an integer p contributes to the covered area if there is some j such that Lj ≤ p
≤ Rj.)
Now, Watson always likes to challenge Sherlock. He has asked Sherlock to remove exactly one interval such that the covered area of the remaining intervals is minimized. Help Sherlock find this minimum possible covered area, after removing exactly one of the N intervals.
Each test case consists of one line with eight integers N, L1, R1, A, B, C1, C2, and M. N is the number of intervals, and the other seven values are parameters that you should use to generate the other intervals, as follows:
First define x1
= L1 and y1
= R1. Then, use the recurrences below to generate xi, yi
for i
= 2 to N:
xi
= ( A*xi-1
+ B*yi-1
+ C1 ) modulo M.yi
= ( A*yi-1
+ B*xi-1
+ C2 ) modulo M.
min(xi, yi)
and Ri = max(xi, yi)
, for all i
= 2 to N.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the minimum possible covered area of all of the intervals remaining after removing exactly one interval.
1 ≤ T ≤ 50.
Memory limit: 1GB.
0 ≤ L1 ≤ R1 ≤ 109.
0 ≤ A ≤ 109.
0 ≤ B ≤ 109.
0 ≤ C1 ≤ 109.
0 ≤ C2 ≤ 109.
1 ≤ M ≤ 109.
Time limit: 30 seconds.
1 ≤ N ≤ 1000.
Time limit: 200 seconds.
1 ≤ N ≤ 5 * 105(500000).
3 1 1 1 1 1 1 1 1 3 2 5 1 2 3 4 10 4 3 4 3 3 8 10 10
Case #1: 0 Case #2: 4 Case #3: 9
Sherlock and Watson have already been introduced to sorting in their computer programming course. Now, Watson has always been curious about parallel computing and wants to sort a permutation of the integers 1 through N by breaking it into chunks, sorting the chunks individually, and then concatenating them.
For a permutation p1, p2, ..., pN
, a chunk is a contiguous subarray of the permutation: i.e., a sequence of elements pi, pi + 1, ..., pj
, for the elements at indexes i and j such that 1 ≤ i
≤ j
≤ N.
Watson wants to partition his permutation into an ordered list of one or more chunks, without changing the order that the elements are in, in such a way that each element of the permutation is in exactly one chunk, and all elements in a chunk are smaller than all elements in any later chunk.
For example, for the permutation [2, 1, 3, 5, 4], these are the only four legal ways for Watson to break it into chunks: [[2, 1, 3], [5, 4]] or [[2, 1], [3, 5, 4]] or [[2, 1], [3], [5, 4]] or [[2, 1, 3, 5, 4]]. Watson is happiest when there are as many chunks as possible; we denote the maximum number of chunks for a permutation p as f(p). In this example, the maximum number of chunks is 3.
Watson wants to consider all permutations p of the numbers 1 through N, and find the sum of squares of f(p). Watson knows Sherlock might come in handy and comes to him for help, but Sherlock is as clueless as Watson and asks you for help. As the sum of squares can be large, please find it modulo M.
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line with two integers N and M.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the sum of squares of f(p)
for all permutations p
of size N, modulo M.
1 ≤ M ≤ 109.
1 ≤ T ≤ 100.
Time limit: 20 seconds.
1 ≤ N ≤ 100.
1 ≤ T ≤ 20.
Time limit: 60 seconds.
1 ≤ N ≤ 5000.
3 1 2 2 4 3 7
Case #1: 1 Case #2: 1 Case #3: 6
f([1]) * f([1]) % 2
= 1.
f([1, 2])
= 2.f([2, 1])
= 1.(22 + 12) % 4
= 1.f([1, 2, 3])
= 3.f([1, 3, 2])
= 2.f([2, 1, 3])
= 2.f([2, 3, 1])
= 1.f([3, 1, 2])
= 1.f([3, 2, 1])
= 1.(32 + 22 + 22 + 12 + 12 + 12) % 7
= 6.