It is said that the hidden treasures on the top of the treasure are very far away. Xiao Ming through the arduous hardships finally found the legend of this Treasure house, the door of the hidden treasure building a plank, the above written a few characters: treasure-hunting instructions.
The contents of the manual are as follows: The Treasure House has a n+1 layer, the topmost layer is the top layer, the top floor has a room hidden treasures. In addition to the top floor, the treasure house another n-storey, each floor m rooms, the M-room surrounded by a circle and in a counterclockwise direction numbered to 0,...,m-1. Some of these rooms have staircases leading up to the upper floors, and each floor may be designed differently. There is a sign in each room, a number x on the sign, indicating that the room starts counterclockwise from this room (assuming the room is numbered K), from the room upstairs, upstairs to the room K of the previous floor. For example, the current room sign on the 2, the counter-clockwise start to try to find the 2nd room with a staircase, from the room upstairs. If the current room itself has a staircase leading to the upper deck, the room is the first room to have a staircase. The end of the treasure hunt is written in a large red font: "Treasure hunt: help you find the number on each floor of the room's sign (i.e. the number on the indicator in the first room in each floor), and the key to open the chest." Please help xiaoming figure out the key to open the chest.
Input:
The first line is 2 integers n and m, separated by a space. n means that in addition to the top floor of the treasure House a total of n-storey building, m means that in addition to the top floor of each floor has m rooms. Next n*m line, two integers per line, separated by a space, each line describes the case in a room, where section (i-1) *m+j lines represent the case of the j-1 room in layer I (i=1,2,..., n;j=1,2,..., M). The first integer indicates whether the room has stairs leading to the previous layer (0 means no, 1 indicates), and the second integer represents the number on the indicator. Note that the room from the staircase of room J to the upper floor must also be room J. The last line, an integer, indicates that Xiaoming entered the treasure house from the bottom of the room to start the hunt (note: The room number starts from 0).
Output:
Output only one line, an integer, indicating the key to open the chest, this number may be very large, please output the results of 20123 modulo.
Input Example:2 3
1 2
0 3
1 4
0 1
1 5
1 2
1Sample output:5 "Input and Output sample description"
First Floor:
Room No. 0, with stairs leading to the upper level, the number on the signage is 2;
Room 1th, no staircase to the upper deck, the number on the signage is 3;
Room 2nd, with stairs leading to the upper level, the number on the signage is 4;
Second floor:
Room No. 0, no staircase to the upper deck, the number on the signage is 1;
Room 1th, with stairs leading to the upper level, the number on the signage is 5;
Room 2nd, with stairs leading to the upper level, the number on the signage is 2;
Xiao Ming first into the first layer (the bottom) of room 1th, note the number on the sign of 3, and then from this room start, along the counter-clockwise to select the 3rd Staircase Room 2nd Room entered, upstairs to reach the second floor of room 2nd, note the number on the sign is 2, because the current room itself has stairs leading to the upper, The room serves as the first room with a staircase. Therefore, at this point in the counterclockwise direction of the 2nd room with a staircase is room 1th, enter the upper staircase to reach the top floor. At this point the above noted signs on the number on the add up, that is, 3+2=5, so the key to open the chest is 5.
"Data range" 0<n≤10000,0<m≤100,0<x≤1,000,000.
In fact, the problem is simulation. But the pure simulation will be time overrun, so you have to go upstairs to mold to optimize.
1#include <iostream>2 using namespacestd;3 intans,n,begin,m,s[10000],map[10000][ -],floor[10000][ -];4 intRead ()5 {6 intx=0, f=1;7 CharCh=GetChar ();8 while(ch<'0'|| Ch>'9')9 {Ten if(ch=='-') f=-1; OneCh=GetChar (); A } - while(ch>='0'&&ch<='9') - { thex=x*Ten+ch-'0'; -Ch=GetChar (); - } - returnx*F; + } - intMain () + { An=read (); atm=read (); - for(intI=0; i<n;i++) - for(intj=0; j<m;j++) - { -Map[i][j]=read (), Floor[i][j]=read ();//input - if(map[i][j]==1) s[i]++;//record a few rooms with stairs in each line in } -begin=read (); to for(intI=0; i<n;i++) + { -Ans+=floor[i][begin];//sum up the numbers on the Staircase room sign theans%=20123;//modulo the answer * if(i==n-1) Break; $ if(floor[i][begin]%s[i]!=0) Floor[i][begin]%=s[i];//record up stairs roomPanax Notoginseng if(floor[i][begin]%s[i]==0) Floor[i][begin]=s[i];//record up stairs room - intK=floor[i][begin];//initial value for staging begin with K the if(map[i][begin]==1) k--;//find a room with a staircase + while(k>0) A { thebegin++; + if(begin==m) begin=0;//If you find your head, look again. - if(map[i][begin]==1) k--;//find a room with a staircase $ } $ } -printf"%d", ans%20123);//modulo the answer - //System ("Pause>nul"); the return 0; -}
C + + answer
Brush over a question of NOIP2012 treasure hunt