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)