#P4548. [CTSC2006] 歌唱王国

    ID: 3508 远端评测题 3000ms 250MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串2006WC/CTSC/集训队枚举,暴力

[CTSC2006] 歌唱王国

Description

In the Singing Kingdom, everyone’s name is a non-empty string consisting only of integers from 1n1\sim n.

There lives a large group of Gurubing soldiers in the kingdom. They gain power by continuously singing the names of the leaders—the “Niuren chieftains”. Each singing process works like this: first, a soldier gets a number from the integer generator, then spends one unit of time to sing this number. If he finds that the name of some Niuren chieftain has already been sung (that is, the name appears as a contiguous substring of the singing sequence), then this singing process ends immediately.

Definitions:

  • Singing sequence: If someone has sung xx numbers, and the number sung at the ii-th step is aia_i, then the singing sequence =(a1,a2,,ax)=(a_1,a_2,\cdots,a_x).
  • Integer generator: A divine object in the Singing Kingdom. It has a button; if you press it once, it returns a random integer from 1n1\sim n with equal probability.
  • Singing time: The time spent in one singing process.

The singing time is random and unpredictable. However, its expected value is fixed. This expected value describes how long the singing time is on average, and can also be called the average singing time.

People in the kingdom love singing. They want the singing time to be as long as possible, so they decide to remove (dismiss) some Niuren chieftains to make the average singing time longer. But they cannot remove all Niuren chieftains; otherwise, each singing process would never stop, and they would not be able to gain power. Therefore, they decide to keep only one Niuren chieftain and remove all the others.

Your task is: given nn, the number of Niuren chieftains tt, and the name of each Niuren chieftain, for each 1it1\leq i\leq t, if only the ii-th Niuren chieftain is kept and all others are removed, output what the average singing time will be.

Hint: This number is a non-negative integer.

Output requirement: Because this number is too large, you only need to output its last 44 digits. If it has fewer than 44 digits, pad leading zeros (see the sample).

Input Format

The first line contains two integers n,tn,t. The following tt lines describe the names of the tt Niuren chieftains.

Line i+1i+1 (1it1\leq i\leq t) has the following format: the first number is mim_i, the length of the ii-th Niuren chieftain’s name. After a space, there are mim_i numbers describing this name, with one space between each pair of adjacent integers.

Output Format

Output tt lines. The ii-th line contains an integer, meaning the last four digits of the maximum average singing time when keeping only the ii-th Niuren chieftain and removing all others.

2 2
1 1
3 1 2 1
0002
0010

Hint

For 100%100\% of the testdata, 1n1051\leq n\leq 10^5, t50t\leq 50, 1mi1051\leq m_i\leq 10^5.

Translated by ChatGPT 5