Domino Cover Problem • Two

Source: Internet
Author: User

Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

Last week we studied the 2xN Domino problem, this week we might as well increase the difficulty, study the 3xN Domino problem?
So our question is: for the 3xN chessboard, how many different coverage methods are used to cover a 1x2 Domino?
First of all we can be sure that the odd length must not be covered, for even lengths, such as 2, 4, we have the following types of coverage:

Input

Line 1th: an integer n. Represents the board length. 1≤n≤100,000,000

Output

Line 1th: An integer representing the number of coverage scenarios MOD 12357

Sample input
62247088
Sample output
4037
Tips:

First, when n is odd, there is no solution, and the output is 0;

When n is even, from a small view:

There are 3 kinds of n=2. Each additional two columns can be considered as a situation:

1. These two columns are not connected with the previous one, and are placed separately, with 3 kinds, namely 3*f (n-2);

2. The two columns are connected to the middle of the previous two columns, that is, the midline is horizontally placed (n-2 and n-1 columns), there is this, you can also put the bottom two transverse to the top, so there are two cases, the latter 4 columns as a whole, namely 2*f (N-4);

And there may be n-3 and n-4 columns, that is, the post 6 columns as a whole, there are two kinds, namely, 2*f (n-6), recursion.

The end of the recursion is F (0) =1;f (2) = 3;

#include <iostream>using namespace Std;typedef unsigned long long ll;const ll MOD = 12357;ll n;ll a[5];void Solve () {    a[0] = 0;    A[1] = 2;    A[2] = 3;    for (int i = 3; I <= N; ++i) {        cout<< (i&1) <<endl;        if (i&1) {                                                        //i is odd            a[i%5] = (2*a[(i-1+5)%5] + a[(i-2+5)%5])% MOD;        } else {                                                            //i is even            a[i%5] = (3*a[( I-2+5)%5] + a[(i-3+5)%5])% MOD;        }    }    cout << a[n%5] << Endl;} int main () {    while (CIN >> N) {        if (N & 1) {            cout << "0" << Endl;        } else {            Solve ();        }    }    return 0;}

Note: The code goes

Domino Cover Problem • Two

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.