06: Treasure Hunt, 06: Treasure Hunt
06: Treasure Hunt
- View
- Submit
- Statistics
- Question
-
Total time limit:
-
2000 ms
-
Memory limit:
-
65536kB
-
Description
-
Legend has it that the top floor of the treasure building is far away from home. James finally found the legendary treasure building, with a wooden board standing at the door of the treasure building. There are several words on it: the treasure guide. The description is as follows:
There are N + 1 floors in the treasure Building, and the top floor is the top floor. There is a room on the top floor that contains the treasure. Apart from the top floor, there are N floors in the treasure building, with M rooms on each floor. The M rooms are circled and numbered 0 in turn in the clockwise direction ,..., M-1. Some of the rooms have stairs leading to the upper floor, and the stairs on each floor may be designed differently. There is a sign in each room, and there is a number x on the sign, which indicates that from this room, you should select the room x with stairs in the counterclockwise direction (assuming the number of the room is k ), go upstairs from the room, and then arrive at room k on the upper floor. For example, if 2 is displayed on the indicator card of the current room, try to find 2nd rooms with stairs and go upstairs from the room. If the current room itself has a stairway leading to the upper layer, this room serves as the first room with a stairway.
At last, the treasure hunt manual was written in a red font: "Notice on Treasure Hunt: help you find the number on the sign (that is, the number on the first sign in the room at each layer) on each floor is the key to open the chest ".
Help James calculate the key to open the chest.
-
Input
-
The two integers N and M in the first line are separated by a space. N indicates a total of N floors except the top floor, and M indicates that there are M rooms on each floor except the top floor.
Next N * M rows, two integers in each line, separated by a space, each line describes the situation in a room, where the (I-1) * M + j indicates the case of room I = 1, 2 ,..., N; j = 1, 2 ,... , M ). The first integer indicates whether the room has a stairway leading to the upper layer (0 indicates no, 1 indicates yes), and the second integer indicates the number on the indicator. Note: The Stairs from room j must climb to the upper floor to arrive at the room j.
In the last line, an integer indicates that James enters the treasure hunt from room number at the bottom of the Building (Note: The room number starts from 0 ).
For 50% of data, there are 0 <N ≤ 10000, 0 <x ≤;
For 100% of data, there are 0 <N ≤ limit, 0 <M ≤ 1,000,000, 0 <x ≤.
-
Output
-
The output contains only one row and an integer, indicating the key to open the treasure chest. This number may be large. Please output the result of Modulo 20123.
-
Sample Input
-
2 31 20 31 40 11 51 21
-
Sample output
-
5
-
Prompt
-
Input and Output sample description:
Level 1:
Room 0 has a stairway leading to the upper layer. The number on the indicator is 2;
Room 1 has no stairs leading to the upper layer. The number on the indicator is 3;
Room 2 has a stairway leading to the upper layer. The number on the indicator is 4;
Layer 2:
Room 0 has no stairs leading to the upper layer. The number on the indicator card is 1;
Room 1 has a stairway leading to the upper layer. The number on the indicator is 5;
Room 2 has a stairway leading to the upper layer. The number on the indicator is 2;
James first Enters Room 1 on the first floor (bottom floor), writes down the number 3 on the sign, and starts from this room, select 3rd rooms with stairs and enter room 2 in the clockwise direction. Go upstairs to Room 2 on the second floor and write down the number on the sign as 2, as the current room itself has a stairway leading to the upper layer, this room serves as the first room with a stairway. Therefore, select Room 1 for the first 2nd stair-mounted rooms in the clockwise direction. after entering the room, go up the stairs to the top floor. In this case, add the numbers on the above signs, namely 3 + 2 = 5, so the key to open the treasure chest is 5.
-
Source
-
Question 2 of NOIP2012 semi-finals popularity Group
-
1 # include <cstdio> 2 # include <cstring> 3 # include <iostream> 4 # include <algorithm> 5 using namespace std; 6 int m, n, I, j, k, x, cnt, ans = 0, now = 0; 7 int a, o [10005] [105] = {0 }, m1 [10005] [105] = {0}; 8 int cnt1 [10005] = {0}; 9 int main () 10 {11 cin >>> n> m; // n floor m: m rooms on each floor 12 for (I = 0; I <n; I ++) 13 {14 cnt = 0; 15 for (j = 0; j <m; j ++) 16 {17 scanf ("% d", & a, & o [I] [j]); // whether a has a stair o sign 18 if (a) 19 m1 [I] [cnt ++] = j; // another store with a stairway (store the corresponding Room) 20 // facilitate data processing after going upstairs 21} 22 cnt1 [I] = cnt; // record how many rooms on each layer have stairs 23} 24 cin> now; // now record the 25 for (I = 0; I <n; I ++) 26 {27 x = o [I] [now]; // x indicates the 28 ans = x + ans on the brand; 29 ans = ans % 20123; // modulo 30 for (k = 0; k <cnt1 [I]; k ++) // find the first room with a stairway on the layer 31 if (m1 [I] [k]> = now) 32 break; 33 now = m1 [I] [(k + x-1) % cnt1 [I]; // remember to subtract one, number of rooms on each layer in the circle 34} 35 cout <ans <endl; 36 return 0; 37}