Example
Example
A kindergarten teacher wants to teach his student on how to paint the sheet of paper. The sheet has the
size 1 × Z and consists of Z consecutive cells numbered from 1 to Z inclusive (we use letter Z because N
will denote another value below and the words "size" and "zoo" both contain letter 'Z' :))
There are also C available colors numbered from 1 to C inclusive. Current color of the i-th cell is Col[i] (1
≤ Col[i] ≤ C).
The student can repaint some cells of the sheet. In a single move he chooses arbitrary pair of integers L
and R such that 1 ≤ L ≤ R ≤ Z and some color Ci from 1 to C inclusive, after that he repaints all the cells on
the segment [L, R] to the color Ci, that is, he assigns Col[j] = Ci for j from L to R inclusive.
IMPORTANT: it is allowed to repaint each cell at most once during the whole sequence of moves.
Help the student to find the total number of different sheets he can get in no more than K moves. Since
the answer can be large, print it modulo 1000000007 (109 + 7).
Two sheets are considered different if there exists at least one index i from 1 to Z inclusive, such that
that A[i] is not equal to B[i], where A[i] is the color of the i-th cell of the first sheet and B[i] is the color of
the i-th cell of the second sheet.
The student, in fact, has some enormous sheet of paper, possibly having billions of cells. But it has very
specific structure. Namely, it consists of several blocks of consecutive cells, where all cells in each
particular block have the same color (possibly different for different blocks), and the number of these
blocks is relatively small. Consider the following example. Let our sheet of paper be {1, 1, 1, 2, 4, 4, 4, 1,
1} (numbers represent colors of the cells). Then it consists of 4 blocks, where the first block has 3 cells of
color 1, the second block has 1 cell of color 2, the third block has 3 cells of color 4 and the last fourth
block has 2 cells of color 1. It seems natural for consecutive blocks to be of different color but we allow
opposite situation in the input too. So please DO NOT assume that two consecutive blocks in the input
are always of different color.
Input
The first line of the input contains a single integer T, the number of test cases. Then T test cases follow.
The first line of each test case contains three space separated integers N, C and K. Here N is the number
of blocks in the sheet, C is the number of available colors and K is the upper bound on the number of
moves the student can make. Each of following N lines contains two space separated integers S[i] and
M[i], where S[i] is the color of the i-th block and M[i] is the number of cells in the i-th block. So the
above value Z equals to M[1] + M[2] + ... + M[N].
Output
For each test case output a single line containing the number of different sheets the student can get
using at most K moves. As was mentioned above you should output this number modulo 109 + 7.
Explanation
Scenario 1: Here the sheet consists of one cell of color 1. We have 47 available colors. Using one move
we can color this cell in any of available colors. So we can get 47 different sheets.
Scenario 2: Here the sheet of paper is {1, 2} (numbers represent colors of the cells). We have 2 available
colors. So there exist 4 different sheets of 2 cells. Using at most one move we can achieve every such
sheet except {2, 1}. Indeed,
import os
from random import randint
tc = "7"
tc = tc + "\n"
for _ in range(7):
#generate N C K
N = randint(1, 7)
C = randint(10, 1000)
K = randint(1, 7)
tc = tc + str(N) + " " + str(C) + " " + str(K)
tc = tc + "\n"
for i in range(N):
#generate S[i] M[i]
S = randint(1, C)
M = randint(1, 1000)
tc = tc + str(S) + " " + str(M)
tc = tc + "\n"
dir_path = os.path.dirname(os.path.realpath(_file_))
file_path = os.path.join(dir_path, "tc.txt")
f = open(file_path, "w")
f.write(tc)
f.close()
f = open(file_path, "r")
print(f.read())
You can run the output solution against the custom input test case generated to get the test case
output