#ABC089D. Practical Skill Test
Practical Skill Test
题目描述
We have a grid with rows and columns. The square at the -th row and the -th column will be called Square .
The integers from through are written throughout the grid, and the integer written in Square is .
You, a magical girl, can teleport a piece placed on Square to Square by consuming magic points.
You now have to take practical tests of your ability as a magical girl.
The -th test will be conducted as follows:
-
Initially, a piece is placed on the square where the integer is written.
-
Let be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer is written, as long as is not . The test ends when .
-
Here, it is guaranteed that is a multiple of .
For each test, find the sum of magic points consumed during that test.
我们有一个行数为 列数为 的网格。位于第 行和第 列的正方形将被称为正方形 。
从 到 的整数写满整个方格,写在方格 中的整数是 。
魔法少女你可以通过消耗 点魔法值,将放置在方格 的棋子传送到方格 。
你现在需要参加 次魔法少女能力的实际测试。
测试方法如下:
- 首先,在写有 整数的方格上放置一枚棋子。
- 假设 是写在棋子所占位置上的整数。只要 不是 ,就重复将棋子移动到写有整数 的位置。当 出现时,测试结束。
- 这里可以保证 是 的倍数。
求每次测试消耗的魔点总和。
输入格式
输入内容按以下格式标准输入:
输出格式
打印每次测试时消耗的魔法值总和。
输出应按照测试的顺序进行。
样例 #1
样例输入 #1
3 3 2
1 4 3
2 5 7
8 9 6
1
4 8
样例输出 #1
5
样例 #2
样例输入 #2
4 2 3
3 7
1 4
5 2
6 8
2
2 2
2 2
样例输出 #2
0
0
样例 #3
样例输入 #3
5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13
样例输出 #3
0
5
0
说明
数据规模与约定
- 是 的倍数。
样例 解释
-
被写入方格 。
-
写在 方格中。
-
写在 方格中。
因此,在第一次测试中消耗的魔法点数总和为 。
样例 解释
请注意,可能有一次测试根本没有移动棋子,也可能有多次相同的测试。