Legend has it that long ago, a mysterious creature lived on the earth: Goblins. Goblins like to live in the Endless Mountains. Specifically, an n-length mountain range h can be divided into n segments from left to right, each with a unique height of hi, where hi is a positive integer between 1 and N. If a mountain range is higher than all the mountains adjacent to it, the mountain range is a mountain. The mountains at the edge have only one contiguous mountain range, others have two segments (i.e. left and right). Similarly, if a mountain range is lower than all its adjacent mountains, the mountain range is a valley. Goblins have a common hobby-drinking, pubs can be set up in the valley. The Goblin's Tavern is always noisy during the day and night, and the scent of the fine wine can float to a radius of the place. Goblin is also a very alert creature, they can set up a lookout on each mountain, and take turns as lookout work to ensure that the first time to know the invasion of foreign enemies. The Goblins hope that each section of the N-section of the mountain can be built into one of the observatory or tavern, and only the entire mountain range that satisfies this condition may be inhabited by the goblins. Now you want to know how many kinds of mountain ranges may be inhabited by N in length. Two Mountains A and b are different when and only if there is an I, which makes Ai≠bi. Since this number may be large, you are only interested in dividing it by the remainder of P.
Exercises
A mountain range with a conditional length of n is the same as the number of species with the beginning of the valley. In this way, we can take half of f[n] to indicate the number of scenarios where an endpoint is a mountain or a valley.
When seeking f[n], consider inserting a mountain with a height of n in the range of n-1 and enumerating where it is inserted, then both sides are discretized and become sub-problems and can be dynamically programmed . Because there are a number of situations on both sides of the composition, multiply the previous combination .
Use the Yang Hui Triangle to find the number of combinations. Note the memory limit, which you use to scroll through the array .
Code:
varI,j,k,l,n,m,p:longint; A:Array[0..4201] ofInt64; C:Array[1..2,0..4201] ofLongint;beginreadln (n,p); c[1,1]:=1; c[1,0]:=1; a[1]:=1; a[0]:=1; fori:=2 toN Do begin forj:=1 toI Do ifJMoD 2=1 ThenA[i]:= (a[i]+ (a[j-1]*c[1, J-1]MoDp) *a[i-j])MoDp; c[2,0]:=1; c[2, i]:=1; forj:=1 toI-1 Doc[2, j]:= (c[1, j]+c[1, J-1])MoDp; forj:=1 toI Doc[1, j]:=c[2, j]; End; Writeln (A[n]*2 MoDp);End.
BZOJ1925 Tribe of Goblins