Lattice brush paint (recursive)

The main idea of ​​the title: a 2*X grid matrix, how many paint schemes are there? You can only brush one grid at a time. After you finish brushing one grid, you can only brush the grids on the left and right or diagonally.

Topic analysis: What is required is the total number of painting sequences.

A C E G I K N P
B D F H J M O Q

Assuming we have the above lattice matrix, we can have the following paint schemes:

1. 1. Brush the first column first, then the second column, then the third column... Corresponding to the table, that is, first brush A, B, then C, D... If you use a [i] represents the number of methods in the first i column starting from an endpoint (assuming A), then in this case, a[i] = a[i-1]*2, because each time you want to judge the first brush above That grid, or the grid below. So a[1] = 1 (by default, start from A), when i>1, a[i] = a[i-1]*2.

2. First brush A, then brush C, then go back to brush B, then brush D, and then start brushing from a certain column in the third column. In this case: a[i] = a[i-2] * 4. Why multiply by 4, it may be A->C->B->D->(E or F), or A ->D->B->C->(E or F). Couldn't it be ACDB? There's really no such thing. If so, after brushing B, how can you brush E or F at intervals?

3. First brush A, then brush C, then brush E, then brush G... That is, select a grid in each column to brush like this, in this case: b[i] = b[i-1]*2, each column has Two cases, b[1]=1. Analysis: Is it possible to return to brush at a certain point in the middle? Impossible, because once returned, the other side cannot be brushed.

Combining the three points of 1.2.3., we get a[i] = (2*a[i-1]+4*a[i-2]+b[i]) * 4, because it can be obtained from A, B, P, Q starts at four points, so you need to multiply by 4.

2. After analyzing the situation that the starting point is four endpoints, then analyze that the endpoint is a certain point in the middle column, assuming that it starts from a certain point above and below the i-th column, the first i-1 column, the number of methods is naturally 2* b[i-1]+2*a[ni], because when I choose column i-1 to start brushing, there are also two choices, whether to choose or not. Similarly, in the following Ni column, the number of methods is 2*b[ni]*2*a[i-1], because the i-th column also has two options, up and down, so the number of methods in this case: 2*(2*b [i-1]*2*a[Ni]+2*a[i-1]*2*b[Ni])

A combination of one or two cases is enough. Note that the intermediate result must be modulo, and the numeric type must be long long, otherwise it will be out of range before the modulo, what should I do?


Code display:

#include <iostream>
using namespace std;

#define max 1000000007

int main(){
    int N;
    cin>>N;
    long long a[1001],b[1001];
    long long sum = 0;
    b[1] = 1;
    for(int i=2;i<=N;i++){
        b[i] = (b[i-1] * 2) % max;
    }
    a[1] = 1;
    a[2] = 6;
    for(int i=3;i<=N;i++){
        a[i] = (2*a[i-1]%max+4*a[i-2]%max+b[i]%max)%max;
    }
    sum = 4*a[N]%max;
    for(int i=2;i<N;i++){
        sum = (sum + 8*a[N-i]*b[i-1]%max+8*a[i-1]*b[N-i]%max)%max;
    }
    cout<<sum<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326014716&siteId=291194637