1225. Flagstime limit:1.0 Second
Memory limit:64 Mbon The day of the "the Flag of Russia a shop-owner decided to decorate the show-window of the" with text Ile stripes of white, blue and red colors. He wants to satisfy the following conditions:
- Stripes of the same color cannot is placed next to all other.
- A blue stripe must always being placed between a white and a red or between a red and a white one.
Determine the number of the ways to fulfill his wish.
Example.For
N= 3 result is Following:input
N, the number of the stripes, 1≤
N≤45.output
M, the number of the ways to decorate the shop-window. Sample
problem Source:2002-2003 ACM Central Region of Russia quarterfinal programming Contest, Rybinsk, October 2002
Source: >
recently asked this question, in fact, is a very simple DP, but someone tangled up why it became Fibonacci. first of all, the idea of DP: There are three kinds of state, white, blue, red, directly correspond to 0, 1, 2 bar, so you can define an array dp[46][3], because the blue only in the white and red, so only one lattice when the initial state is: dp[1][0]=dp[1][2]=1,dp[1 ][1]=0. fornext to each lattice, this one is red depending on the previous lattice is not red, this one is white depends on the previous lattice is not white; also assume that if the previous one is blue, then this one is red/white depending on the front of the second lattice is not red/white, so there is the following recursion: White: dp[i][0]=dp[i-1][2]+dp[i-2][2]; Blue: dp[i][1]=dp[i-1][0]+dp[i-1][2];
red: dp[i][2]=dp[i-1][0]+dp[i-2][0];
finally add dp[n][0] and dp[n][2] together is the sum of all the cases, because the last one can not be blue anyway.
1 intmain2 () {2 intNLong Longdp[ $][3]={0};3dp[1][0]=dp[1][2]=1;4scanf"%d", &N);5 for(intI=2; i<=n; i++)6dp[i][0]=dp[i-2][2]+dp[i-1][2],7dp[i][1]=dp[i-1][0]+dp[i-1][2],8dp[i][2]=dp[i-2][0]+dp[i-1][0];9printf"%lld\n", dp[n][0]+dp[n][2]);Ten return 0; One}
then we can find some interesting things, in fact, the white and red recursion is interdependent, and the blue does not have any use, because this one is blue depends on the previous box is not blue, that is, the former is a white or red case sum, this number does not provide the next grid for the first two of the effective judgment. careful observation found that the original white and red is two of the same Fibonacci sequence, so good to do, two synthetic one, F[1]=f[2]=2,f[i]=f[i-1]+f[i-2], the last f[n] is the sum of N lattice.
1 intMain () {2 intNLong Longdp[ $]={0,2,2};3scanf"%d", &N);4 for(intI=3; i<=n; i++)5dp[i]=dp[i-1]+dp[i-2];6printf"%lld\n", Dp[n]);7 return 0;8}
at the end of the day, how did Fibonacci do it? First ignore the blue, the first lattice f[1]=2, only white/red two cases, because the white/red can not appear continuously, so this is what, has been dead to the next lattice is what, so the first lattice f[i]=f[i-1]. And then see what happens when you join blue: If the first one is blue, then the current lattice must be different from the first two, then f[i]=f[i-2]; In a comprehensive consideration, f[i]=f[i-1]+f[i-2].
Ural 1225. Flags