Codeforces 1248 C. Ivan the Fool and the Probability Theory (思维) div2

  Meaning of the questions: to a grid of n × m, painted black and white only, no case of three asked Unicom squares painted, how many coating? The results mod 1e9 + 7

  

  Direct Solution to say:

   Analyzing the first case of the first row, do recurrence relation F [i], F [1] is obviously 2, F [2] may also be arbitrary, apparently 4.

   Then F [3], the first two points need to be continuous if the same color, the same color if it is continuous, then only a color different from the previous. We F [i] is extended to F [i] [j], i denotes the number of the first block, j denotes the i th of whether the first two consecutive two. Then the number of kinds of F [i] is divided into:

    (1): in front of two consecutive identical, then F [i] of number of species and F [i - 1] several identical, and the state becomes discontinuous. That is: F [i] [0] + = F [i - 1] [1];

      (2): first two successive different, then the current two cases, continue different from the previous, or the same as before, into the continuous state. Either: F [i] [0] + = F [i - 1] [0], F [i] [1] = F [i - 1] [0];

    To obtain the total recurrence relations:

        F[ i ] [ 0 ]  = F[ i - 1 ] [ 0 ] + F[ i -1 ][ 1];

        F[ i ] [ 1 ]  = F[ i - 1 ] [ 0 ];

      F[ 1 ][ 0 ] = 2; F[ 1 ][ 1 ]=0;

   If it is the i-th block, then there is [1] Species Solutions F [i] [0] + F [i];

 // In fact, the way can also be used as coloring of N steps, each time on an order or second-order, ask how many there are on the ladder method.

  Two understandings are talking about ways recurrence relations pointing Feibonaqi. Release of both the n items of the m-th item Feibonaqi.

  After determining the first line, and then we recursive to all the lines.

    If the first line has two consecutive color, then each following line must exactly opposite to the previous line. If you have the same three consecutive same situation so inevitable.

      Proof: If two consecutive on the same line, the next line is not opposite, then there must be a continuous one and the same two identical, even into three

         If the line is not the same at two consecutive, there must be a contrast to the incomplete line

          Set on a black and white black and white black behavior

          Then the next line must have a black or white to black and white, will be the same as the right and left, and even into three identical.

          If they are identical, the two will inevitably encounter the same level are connected thereto

              White black white black and white

              Black and White Black and White Black

          Be identical or below Eren black and above white Eren

       Therefore, only one of the following results, each opposite to the previous row.

    If the first row is not two consecutive identical color, either black and white or monochrome black and white monochrome black and white monochrome black and white monochrome (two kinds in total), then the next line can be completely equal to the bank, or the opposite. Two identical and only the continuous lines. This also corresponds to each row Recursive down, the same is no continuous three rows, i.e., the top row and the same extrapolation, the value of m is Feibonaqi item. Since the initial state of the two is not satisfied only two consecutive identical color, it is 2 × m Feibonaqi

  Then the result is the first line of continuous color, all the remaining rows in the result has been fixed. A total of 2 × n -2 item Feibonaqi

         The first line of non-continuous color, the remaining item type is Feibonaqi m, 2 × both Feibonaqi item m

The total number of results for the 2 × (Feibonaqi Feibonaqi m + n item item) -2

 

The following is the code AC (not found rows is also Feibonaqi, so complicated points a game.)  

 1 #include<iostream>
 2 using namespace std;
 3 typedef long long ll;
 4 const int mod = 1e9+7;
 5 const int maxn = 1e5+50;
 6 ll dp[maxn][3];
 7 int main(){
 8     int n,m;
 9     cin>>n>>m;
10     dp[1][0]=2;
11     dp[1][1]=0;                          //初始化
12     for(int i=2;i<=n;i++){                //递推
13         dp[i][0]=dp[i-1][0]+dp[i-1][1];
14         dp[i][1]=dp[i-1][0];
15         dp[i][0]%=mod;
16         dp[i][1]%=mod;
17     }
18     ll a,b,c;
19     a=0,b=1,c=0;
20     for(int i=0;i<n;i++){    
21         c=a+b;
22         a=b;
23         b=c;
24         c%=mod;
25     }
26     ll res = dp[m][0]+dp[m][1]-2+b*2;    //得结果
27     res%=mod;
28     cout<<res<<endl;
29     return 0;
30 }

 

 

   

      

Guess you like

Origin www.cnblogs.com/greenpepper/p/11711071.html