Algorithm Note _185: Previous questions lattice brush paint (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description Problem DescriptionThe top of an ancient city wall in the X country can be seen as a rectangle of 2*n squares (as shown), which now needs to be painted with protective paint.


you can brush from any one of the grid, brush a lattice, you can move to the adjacent lattice (diagonal adjacent also arithmetic), but can not move to a farther lattice (because the paint is not dry can not step!) )
For example: a D b c E F is a qualified paint order.
c E F D A B is another appropriate option.
when N is known, the total number of scenarios is calculated. When n is large, the result increases rapidly, and the result is 1000000007 (1,000,000,007) modulo. Input Formatinput data is a positive integer (not greater than +)output Formatthe output data is a positive integer. Sample Input2Sample Output -Sample Input3Sample Output theSample Input ASample Output359635897

2 Solutions

The specific code is as follows:

ImportJava.util.Scanner; Public classMain { Public Static LongMOD = 1000000007;  Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intn =In.nextint (); Long[] q1 =New Long[n + 1]; Long[] q2 =New Long[n + 1]; q1[1] = 1; q2[1] = 1; q1[2] = 2; q2[2] = 6;  for(inti = 3;i <= n;i++) {Q1[i]= 2 * q1[i-1]; Q1[i]%=MOD; Q2[i]= 2 * Q1[i-1] + 2 * q2[i-1] + 4 * q2[i-2]; Q2[i]%=MOD; }        Longsum = (4 * q2[n])%MOD;  for(inti = 2;i < n;i++) {sum= (sum + 4 * ((q1[i] * q2[n-i])% mod + (q1[n-i + 1] * q2[i-1])% MoD)MOD; }        if(n = = 1) Sum= 2;    SYSTEM.OUT.PRINTLN (sum); }}

Resources:

1. Blue Bridge cup check brush paint

Algorithm Note _185: Previous questions lattice brush paint (Java)

Related Article

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.