Gambling and rectangles
Time limit1sMemory limit256 MB
Compute the expected score over all equally likely rectangles, where the score squares the count of each value 1 to 5, and print it as a reduced fraction.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Prefix sum
- Solved
- No attempts yet
Problem
An by board is made of 1×1 square cells arranged in a square, and each cell holds one integer between 1 and 5. You want to run a gambling house with this board.

A possible board when
The game works like this. First, extend every side of the board to draw vertical lines and horizontal lines. Then ask a customer to pick 2 distinct horizontal lines and 2 distinct vertical lines. The customer does not know where the lines are drawn, so the pick is one of the possibilities, each equally likely.

The board has 5 vertical lines and 5 horizontal lines, and the customer picks two vertical lines and two horizontal lines
You then score the rectangle enclosed by the four chosen lines and pay the customer that amount. The scoring rule is simple. Let , , , , be how many 1s, 2s, 3s, 4s and 5s lie inside the rectangle. The score the customer receives is
Because the counts are squared, the score grows much faster as the customer's rectangle grows. That raises the stakes and helps attract more customers. All that is left is setting the wager, and for that you need the expected value of the score .
Given the board, write a program that computes the expected value of .
Input
The first line contains an integer (), the size of the board.
Each of the next lines contains integers between 1 and 5, separated by spaces. The -th number on the -th line (, ) is the number written in row from the top and column from the left.
Output
Print the expected value of on the first line as an irreducible fraction in the form p/q. Here and are integers, , and the greatest common divisor of and is 1. Print the denominator even when the expected value is an integer, so the form is p/1. For example, print 10/3 when the expected value is , and 3/1 when it is 3.