#P1076. [NOIP 2012 普及组] 寻宝

    ID: 76 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>模拟2012NOIp 普及组概率论,统计

[NOIP 2012 普及组] 寻宝

Description

It is said that a tempting treasure is hidden on the top floor of a distant Treasure Tower. After many hardships, Xiao Ming finally finds the legendary Treasure Tower. At the entrance stands a wooden board with big characters: Treasure Hunt Manual. The manual reads as follows:

The Treasure Tower has N+1N+1 floors. The topmost floor is the top floor, and there is a room on the top floor that contains the treasure. Except for the top floor, there are NN other floors, each with MM rooms. These MM rooms form a circle and are numbered in counterclockwise order as 0,,M10,\cdots,M-1. Some rooms have stairs leading to the floor above, and the stair layout may be different on each floor. In each room there is a signboard with a number xx. Starting from this room, choose the xx-th room with stairs in counterclockwise order (suppose its index is kk), go upstairs from that room, and after going up you will arrive at room kk on the floor above. For example, if the signboard in the current room says 22, then start searching counterclockwise and find the second room that has stairs, and go upstairs from that room. If the current room itself has stairs to the upper floor, it is counted as the first room with stairs.

At the end of the manual, in large red characters, it says: "Treasure Hunt Notice: The sum of the numbers on the signboards that help you find the room to go upstairs on each floor (that is, the number on the signboard in the first room entered on each floor) is the key to open the treasure chest."

Please help Xiao Ming compute this key to open the treasure chest.

Input Format

The first line contains two integers NN and MM, separated by a space. NN means there are NN floors other than the top floor, and MM means there are MM rooms on each floor other than the top floor.

The next N×MN \times M lines each contain two integers separated by a space, describing the situation in one room. The (i1)×M+j(i-1) \times M + j-th line describes room j1j-1 on floor ii (i=1,2,,Ni=1,2,\cdots,N; j=1,2,,Mj=1,2,\cdots,M). The first integer indicates whether this room has stairs to the floor above (00 means no, 11 means yes). The second integer is the number on the signboard. Note that if you climb the stairs from room jj to the floor above, you will arrive at room jj on that floor.

The last line contains one integer, which is the index of the room on the bottom floor where Xiao Ming starts the treasure hunt (note: room indices start from 00).

Output Format

Output one integer: the key to open the treasure chest. This number can be very large, so output it modulo 2012320123.

2 3
1 2
0 3
1 4
0 1
1 5
1 2
1

5

Hint

Constraints

For 50%50\% of the testdata, 0<N10000 < N \le 1000, 0<x1040 < x \le 10^4.
For 100%100\% of the testdata, 0<N100000 < N \le 10000, 0<M1000 < M \le 100, 0<x1060 < x \le 10^6.

NOIP 2012 Junior, Problem 2.

Translated by ChatGPT 5