A Year of More Contests
Time limit5sMemory limit512 MB
Given T tournaments with fixed round offsets, each starting on a uniformly random day among N, compute the exact expected sum of squared daily round counts.
- Level
Medium7 of 10
- Topics
- Probability, Math, Combinatorics
- Solved
- No attempts yet
Problem
A new year brings a new calendar and a fresh run of programming contests. Sphinny plans her year around them again.
Sphinny follows several tournaments. Each tournament has a fixed number of rounds, and the organizer has already fixed how many days after the tournament start each round takes place. The start date itself is still undecided.
Rounds of different tournaments can land on the same day. Sphinny is happier when a day holds more rounds. For every day that holds rounds her happiness grows by . Her happiness starts at 0.
The picture below shows three tournaments in three colors. One starts on day 2 of the year, one on day 5, and one on day 6, and Sphinny's happiness adds up to 20.

The year has days. Each tournament starts on one of those days, every day is equally likely, and the tournaments are independent of each other. Compute the expected value of Sphinny's happiness.
Sphinny does not want an approximation. There are tournaments and equally likely ways to pick the start dates. Write the expected happiness as , where and are positive integers and is a non-negative integer smaller than . If is 0 then must be 1, and otherwise the greatest common divisor of and must be 1.
A tournament that starts late pushes some of its rounds into the next year. Rounds that fall into the next year do not add to this year's happiness.
Input
The first line holds the number of test cases . The first line of each test case has the form
N T
where is the number of days in the year and is the number of tournaments. Then lines follow, one per tournament, in the form
m d2 d3 ... dm
This tournament has rounds, and its -th round is held on day counted from the start of that tournament. The first round is always on day 1, so is not printed.
Limits
Output
For each test case print one line of the form
Case #X: K+A/B
where is the case number starting from 1, and , , are as described in the statement.