NOIP201202 Treasure Hunt

Source: Internet
Author: User
Tags ming

It should have been a question of yesterday.

Question Description

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
1
Output example
5
Other Notes
"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.

This problem is "very good fun", but if you can not understand the problem will certainly not be done. So the main problem is to study the ability to read. There are ways to avoid time overrun.

1#include <iostream>2 3 using namespacestd;4 intc[10010],a[10010][101],b[10001][101];5 intRead ()//Enter a template that is important in the subject6 {7     intx=0, f=1;8     CharCh=GetChar ();9      while(ch<'0'|| Ch>'9')Ten     { One         if(ch=='-') f=-1; ACh=GetChar (); -     } -      while(ch>='0'&&ch<='9') the     { -x=x*Ten+ch-'0'; -Ch=GetChar (); -     } +     returnx*F; - }  + intMain () A { at     inti,j,n,m,t; -N=read (); m=read (); -      for(i=1; i<=n;i++) -     { -          for(j=0; j<m;j++) {a[i][j]=read (); B[i][j]=read ();if(a[i][j]==1) c[i]++;}//A array indicates whether the current room has a staircase, b array indicates the current room's sign -     } int=read (); -j=T; to     intans=0, temp=0; +      for(i=1; i<=n;i++) -     { theans+=b[i][t];ans%=20123; *j=T; $temp=0;Panax Notoginseng         if(b[i][t]%c[i]==0) B[i][t]=c[i];//to prevent the indicator from being a number that is many times larger than M, the loop is too many times overrun -         Elseb[i][t]=b[i][t]%C[i]; the         if(a[i][t]==1) temp++;//if the starting point has a staircase, subtract the current staircase +        //cout<< "ans" <<ans<<endl; A         while(Temp<b[i][t])//find the starting position of the second layer the        { +J + +; -            if(j>=m) j=0; $            if(a[i][j]==1) temp++; $        } -t=J; -     } thecout<<ans; -     return 0;Wuyi}
NOIP201202 Treasure Hunt

NOIP201202 Treasure Hunt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.