NOIP2012 Universal Group

Source: Internet
Author: User

T2:

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 the n+1 layer, the topmost layer is the top layer, the top floor has a room inside 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/output format
Input format:
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 format:
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/Output sample
Input Sample # #:

2 3
1 2
0 3
1 4
0 1
1 5
1 2
1
Sample # # of output:
5

"Data Range"
For 50% data, there are 0<n≤1000,0<x≤10000;
For 100% data, there is 0<n≤10000,0<m≤100,0<x≤1,000,000.



This topic is actually a simulation problem, in which if the method with pure simulation can only get 50 points, so can be optimized.

To determine which staircase to arrive at the time of the room can use the principle of the cycle, only to take the value of the modulus to simulate can-this can greatly reduce the complexity.

Code:

var bz,num:array[0..10000,0..100] of Longint;         C:ARRAY[0..10000] of Longint; I,j,n,m,w,ans,sum:longint;begin assign (input, ' treasure.in ');         Reset (input); Assign (output, ' treasure.out ');         Rewrite (output);         READLN (N,M); For I:=1 to N does for j:=1 to M do begin READLN (bz[i,j],num[i,j                           ]);                  If Bz[i,j]=1 then Inc (C[i]);         End  READLN (w);         Inc (W);                  For I:=1 to n do begin ans:= (Ans+num[i,w]) mod 20123;                  j:= (num[i,w]-1) mod c[i]+1;                  SUM:=BZ[I,W];                           While sum<>j do begin w:=w MoD m+1;                  Inc (Sum,bz[i,w]);         End         End         Writeln (ANS); Close (input); close (output); end.


Of course, the time complexity of the above code is O (nm), but if M is a little larger, that is, the O (n) algorithm--can be optimized again, the value after the modulo is judged which staircase can be optimized, optimized to O (1) calculation, A[i,t] is the current staircase, It is mainly noted that f[i,j] represents the number of staircase rooms to which the first floor to the J room is cut off. f[i,w]+ (Tot[i,w] mod a[i,0]) is the required value. Secondly, it is also important to note that the value of T = 0, when W is equal to the last room of layer I.

Code (but in essence the data time for this problem does not reflect much, if M is larger then this method must be used):

var n,m,i,j,w,ans,t:longint;        BZ:ARRAY[1..10000,0..100] of 0..1; A,TOT,F:ARRAY[1..10000,0..100] of Longint;begin assign (input, ' treasure.in ');        Reset (input); Assign (output, ' treasure.out ');        Rewrite (output);        READLN (N,M);                        For I:=1 to N does for j:=1 to M do begin READLN (Bz[i,j],tot[i,j]);                        F[I,J]:=F[I,J-1];                                If Bz[i,j]=1 then BEGIN Inc (a[i,0]);                                A[i,a[i,0]]:=j;                        Inc (F[i,j]);                End        End        READLN (W);        Inc (W);                For I:=1 to n do begin ans:= (Ans+tot[i,w]) mod 20123;                If Bz[i,w]=1 then Dec (tot[i,w]);                T:= ((tot[i,w] mod a[i,0]) +f[i,w]-1) mod a[i,0]+1;        If T<>0 then w:=a[i,t] else w:=a[i,a[i,0]];        End Writeln (ANS); Close (input); Close (output); end.



T2:

Xiao Ming's flower Shop opened new, in order to attract customers, he wanted to put a row of flowers at the door of the florist, a total of M basin. By investigating customers ' preferences, Xiao Ming lists the customers ' favorite n flowers, numbered from 1 to N. In order to show more flowers at the door, I can not grow more flowers than the AI basin, put flowers together with the same flowers, and different types of flowers need to be labeled from small to large order.
Test programming calculation, there are a total of how many different flower schemes.


Input/output format
Input format:
The first line consists of two positive integers n and m, separated by a space in the middle.
The second line has n integers, separated by a space between each two integers, which in turn represent A1, A2 、...... an.

Output format:
The output has only one row, an integer, indicating how many scenarios. Note: Because the number of scenarios is likely to be large, output the result of modulo 1000007 for the number of scenarios.


Input/Output sample
Input Sample # #:
2 4
3 2
Sample # # of output:
2

Description
"Data Range"
For 20% data, there are 0<n≤8,0<m≤8,0≤ai≤8;
For 50% data, there are 0<n≤20,0<m≤20,0≤ai≤20;
For 100% data, there is 0<n≤100,0<m≤100,0≤ai≤100.


This topic is the naked DP, the state is also very easy to think of-set f[i,j] that the pendulum I flower in the first J position of the program number, obviously f[i,j]:=f[i,j]+f[i-1,j-k]. Border F[i,0]:=1;


Code:

var        a:array[1..100] of Longint;        F:ARRAY[0..100,0..100] of Longint;        N,m,i,j,k:longint;function min (x,y:longint): Longint;begin        if X<y then exit (x) Else exit (y); End;begin        Assign (input, ' flower.in '); Reset (input);        Assign (output, ' flower.out '); Rewrite (output);        Read (n,m);        For I:=1 to n do                read (A[i]);        For i:=0 to n do                f[i,0]:=1;        For I:=1-to-n do-                j:=1 to                        -m do-k:=0 to Min (J,a[i]) do                                f[i,j]:= (f[i,j]+f[i-1,j-k]) mod 1000007;        Writeln (F[n,m]);        Close (input); Close (output); end.



T4:

There is a messenger who travels to all countries, he can learn a culture in every country, But he does not want to learn any culture more than once (that is, if he learns a certain culture, he will not be able to reach other countries with this culture). Different countries may have the same culture. Different cultures have different perceptions of other cultures, and some cultures exclude foreign cultures (that is, if he learns a certain culture, he cannot reach other countries that exclude it). Given the geographical relations among nations, the cultures of each country, the views of each culture on other cultures, and the beginning and end of the messenger's Journey (learning the local culture at the beginning and end), the road distance between countries, and the minimum number of roads to be traveled from the beginning to the end.

Input Output format
Input format:
The first behavior is five integers n,k,m,s,t, each two integers separated by a space, which in turn represents the country
number (country number 1 to N), the number of cultural species (Culture number 1 to K), the number of roads, And the number of the starting and ending points (guaranteed S not equal to T);
The second behavior N integers, each two integers separated by a space, the number I CI, which indicates that the culture of country I is CI.
the next K-line, each line of k integers, each two integers separated by a space, the first row of the number of J is aij,aij= 1 for the culture I exclude foreign culture J (I equals J when the foreigner to reject the same culture), aij= 0 means not to repel (note I repel J does not guarantee that J must also repel I).
the next M-line, each line of three integers u,v,d, each two integers separated by a space, indicating that the country U and the State v has a two-way road with a distance of D (guaranteed u not equal to V, there may be multiple roads between two countries).

Output format: The output is only one row, an integer, indicating the number of distances the messenger must travel from the country of origin to the destination country (if no solution is output-1).

Input and Output sample
Input Sample # #:
Input and Output sample   1

2 2 1) 1 2
1 2
0 1
1 0
1 2 10

Input/Output Example 2

2 2 1) 1 2
1 2
0 1
0 0
1 2 10


Sample # # of output:
Input/Output Example 1

-1

Input/Output Example 2

10


Description
Input/Output Sample Description 1
Since to the country 2 must go through the state 1, and the country 2 of the civilization has excluded the state 1 civilization, it is impossible to reach the country 2. Input/Output Sample description 2 Route 1-2

"Data Range"
For 100% of data, there are 2≤n≤100 1≤k≤100 1≤m≤n2 1≤ki≤k 1≤u, v≤n 1≤d≤1000 s≠t 1≤s,t≤n


This problem data is very pit, and the pit is rotten, for example, "correct" DFS reverse search on the line, here the correct why to quote? Because there is a provision in the topic is not to learn the repetition of the culture, there is no culture can not be excluded, and not only 22 cities do not exclude, and these two rules even if not add the same can ac--because there is no such a two rules, that the problem with Floyd can also do the right! --but the problem of the positive solution I think is SPFA, because the data if bigger, strong point, Dfs is too much, and Floyd answer is wrong, only SPFA can do right, SPFA inside need a lot of sentence weight.

For example,--DIS[I].BZ is a culture that judges cannot learn to repeat, while Dis[i].xuan is the chosen culture that cannot be excluded. If the two sentences do not know where the difficulties are, however, even if there are these two judgments as if this is not a difficult point.


Method of SPFA algorithm:

Type arr=array[0..100] of Longint;var c:array[1..100] of Longint;        A:ARRAY[1..100,0..1000] of Longint;        BZ,D:ARRAY[1..100,1..100] of Longint;        DATA:ARRAY[1..1000] of Longint;        F:ARRAY[1..100] of Boolean; DIS:ARRAY[1..100] of record way:longint;        Xuan:arr end;  N,k,m,s,t,i,j,x,y,way,head,tail,w:longint;function PD (X:arr; k:longint): Boolean;var i:LONGINT;begin for I:=1        To X[0] does if (bz[c[k],c[x[i]]]=1) then exit (false); Exit (TRUE); End;begin assign (input, ' culture.in ');        Reset (input); Assign (output, ' culture.out ');        Rewrite (output);        READLN (n,k,m,s,t);        For I:=1 to n do read (C[i]);                For I:=1-to-K do-to-j:=1-K do-read (Bz[i,j]);        READLN;        End        Fillchar (d,sizeof (d), 5);                For I:=1 to M do begin read (X,y,way);                Inc (a[x,0]); a[x,a[X,0]]:=y;                If way<d[x,y] then D[x,y]:=way;                Inc (a[y,0]);                A[y,a[y,0]]:=x;        If way<d[y,x] then D[y,x]:=way;        End        head:=0;        Tail:=1;        Fillchar (dis,sizeof (DIS), 5);        dis[s].way:=0;        Dis[s].xuan[0]:=1;        Dis[s].xuan[1]:=s;        Data[1]:=s;                While Head<tail DO BEGIN Inc (head);                W:=data[head]; For I:=1 to a[w,0] do if PD (Dis[w].xuan,a[w,i]) and (Dis[a[w,i]].way>dis[w].way+d[w,a[w,i]) the                                n Begin dis[a[w,i]]:=dis[w];                                Dis[a[w,i]].way:=dis[a[w,i]].way+d[w,a[w,i]];                                Inc (Dis[a[w,i]].xuan[0]);                                Dis[a[w,i]].xuan[dis[a[w,i]].xuan[0]]:=a[w,i];                                       If not f[a[w,i]] then BEGIN F[a[w,i]]:=true;                                        Inc (tail);                                Data[tail]:=a[w,i];                        End                End        F[w]:=false;        End        If dis[t].way=84215045 then Writeln ( -1) Else Writeln (Dis[t].way); Close (input); Close (output); end.


NOIP2012 Universal Group

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.