Cucumber Boy and Cucumber Girl

All submissions for this problem are available.

Long ago in Wolf Town, there were N kinds of pizza shops each selling one ingredient of pizza. Let ingredients and pizza shops be numbered from 1 to N, then pizza shop k selling ingredient k. They had never invaded other shops, namely, they had kept to sell only one ingredient. But the situation changed after some shop violates the unspoken rule. Then all of N pizza shops start to sell all N ingredients and they are competing.

As time past, a pizza is now one of the most popular foods, and every pair of pizza shops is collaborated for improving quality. Let pizza shop (j1, j2) denotes the shop collaborated between shops j1 and j2. There are B pizza buildings having every pizza shop, including collaborated shops. The price of a pizza is determined by its quality.

Let the quality of ingredient k of pizza shop j in building i be Q[i, j, k]. Then the quality of ingredient k of pizza shop (j1, j2) in building i is Q[i, j1, k] * Q[i, j2, k]. The price of a pizza is the summation of qualities of its ingredients.

By the way, Cucumber Boy and Cucumber Girl live in the Wolf Town. Cucumber Boy and Cucumber Girl like pizza very much, but now they are bored by the existing pizzas, despite there are many kinds of pizzas. So they want to propose the collaboration between two distinct buildings (say i1 and i2). If their wish is realized, the pizza collaborated between shop j1 in building i1 and j2 in building i2 will be sold. The price of the pizza will be determined by the usual way, that is, the price will bethe summation of Q[i1, j1, k] * Q[i2, j2, k] over 1 ≤ k ≤ N.Note that j1 = j2 is possible here.

Now Cucumber Boy chooses two buildings i1 and i2 such that 1 ≤ i1 < i2 ≤ B. In each day Cucumber Boy and Cucumber Girl want to eat N collaborated pizzas. Here these N pizzas should satisfy the following conditions.

Each of N pizzas is a collaborated pizza between one shop in building i1 and one shop in building i2.

Every shop in building i1 collaborates for one of N pizzas.

Every shop in building i2 collaborates for one of N pizzas.

Clearly, there are exactly N! (factorial of N) of such sets of N pizzas. Therefore, Cucumber Boy and Cucumber Girl will eat all distinct such sets during N! days eating each such set of pizzas exactly once and, of course, eating exactly one such set per day (see the first sample input for clarity).

Cucumber Boy is a show-off, so he wants to pay more money than Cucumber Girl. But Cucumber Girl is a steadfast person, so she may disturb it. However if at least one of N pizzas has odd price (that is, the price not divisible by 2), she gives away him to pay more money than her in this day (otherwise, she pays the same amount of money as he pays). But the next time such situation occurs, she will pay more money than Cucumber Boy in this day. Namely, consider all days for which we have at least one pizza with odd price and number them starting from 1. Then in the (2*x−1)-th such day Cucumber Boy pays more money than Cucumber Girl, and in the (2*x)-th such day, Cucumber Girl pays more money than Cucumber Boy. That is, at such days with numbers 1, 3, 5, ... Cucumber Boy pays more money than Cucumber Girl, while at such days with numbers 2, 4, 6, ... we have the opposite situation. So now Cucumber Boy's objective will be accomplished ifthe number of times that he pays more money than her greater than the number of times that he pays less than her.

Now your task is to calculate the number of pairs of buildings for which Cucumber Boy's objective will be accomplished.

Input

The first line contains an integer T denoting the number of test cases. Then T test cases follow.

The first line of each test case contains 2 space-separated integers N and B. The description of each B buildings follows.

The first line for each building i contains one string, which is raw or random. If this string is raw, then N lines follow, each line contains N space-separated integers, where the k-th integer of the j-th line denotes Q[i, j, k]. If this string is random, then a single line follows containing 3 space-separated integers Si, Pi and Ai. Let the array Xi be defined as Xi[1] = Si, Xi[k] = (Pi * Xi[k−1] + Ai) modulo M, where M = 232 = 4294967296. Then Q[i, j, k] is calculated as (floor(Xi[(j−1)*N+k] / D) modulo 100) + 1, where D = 212 = 4096.

Output

For each test case, output a single line containing the number of pairs (i1, i2) of buildings with 1 ≤ i1 < i2 ≤ B for which Cucumber Boy's objective will be accomplished.

Explanation

In the first sample case, Cucumber Boy's objective will be accomplished if and only if he selects buildings (i1, i2) = (1, 2). Let's call the pizza of the shop x of the building 1 collaborated with the shop y of the building 2 as pizza (x, y). Thenthe price of the pizza (1, 1) is 82*19+51*84+44*64 = 8658,the price of the pizza (1, 2) is 82*17+51*43+44*44 = 5523,the price of the pizza (1, 3) is 82*30+51*81+44*57 = 9099,the price of the pizza (2, 1) is 41*19+10*84+38*64 = 4051,the price of the pizza (2, 2) is 41*17+10*43+38*44 = 2799,the price of the pizza (2, 3) is 41*30+10*81+38*57 = 4206,the price of the pizza (3, 1) is 23*19+33*84+58*64 = 6921,the price of the pizza (3, 2) is 23*17+33*43+58*44 = 4362,the price of the pizza (3, 3) is 23*30+33*81+58*57 = 6669.There will be 5 days for which at least one of the corresponding pizzas has odd price as follows:pizzas (1, 1), (2, 2), (3, 3),pizzas (1, 2), (2, 1), (3, 3),pizzas (1, 2), (2, 3), (3, 1),pizzas (1, 3), (2, 1), (3, 2),pizzas (1, 3), (2, 2), (3, 1).Therefore Cucumber boy will pay more money than Cucumber girl in 3 of these 5 days, and Cucumber boy will pay less in 2 of these 5 days. Note that all pizzas (1, 1), (2, 3), (3, 2) have even price, hence Cucumber Boy and Cucumber Girl pay the same amount of money in this day.

Note that in the third sample case we have building 1 given as row input and building 2 given as random input. This is only to emphasize that such mix is possible. You should figure it out by yourself why for the only pair of buildings in this example Cucumber Boy's objective will be accomplished.

Comments

Please login at the top to post a comment.

SUCCESSFUL SUBMISSIONS

Fetching successful submissions

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.

Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.

Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.

Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.

Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to INR 20,000 (for Indian Community), $700 (for Global Community) and lots more CodeChef goodies up for grabs.