问题描述
X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形(如下图所示),现需要把这些格子刷上保护漆。
你可以从任意一个格子刷起,刷完一格,可以移动到和它相邻的格子(对角相邻也算数),但不能移动到较远的格子(因为油漆未干不能踩!)
比如:a d b c e f 就是合格的刷漆顺序。
c e f d a b 是另一种合适的方案。
当已知 N 时,求总的方案数。当N较大时,结果会迅速增大,请把结果对 1000000007 (十亿零七) 取模。
![](https://i-blog.csdnimg.cn/blog_migrate/4b9456b25b4159fd078c16b07768872e.png)
你可以从任意一个格子刷起,刷完一格,可以移动到和它相邻的格子(对角相邻也算数),但不能移动到较远的格子(因为油漆未干不能踩!)
比如:a d b c e f 就是合格的刷漆顺序。
c e f d a b 是另一种合适的方案。
当已知 N 时,求总的方案数。当N较大时,结果会迅速增大,请把结果对 1000000007 (十亿零七) 取模。
输入格式
输入数据为一个正整数(不大于1000)
输出格式
输出数据为一个正整数。
样例输入
2
样例输出
24
样例输入
3
样例输出
96
样例输入
22
样例输出
359635897
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static BigInteger [] a = new BigInteger[1001];
public static BigInteger [] b = new BigInteger[1001];
public static BigInteger [] temp = new BigInteger[1001];
public static BigInteger temp1 ;
public static BigInteger temp2 ;
public static BigInteger sum;
public static final BigInteger NUM = new BigInteger(1000000007+"");
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
b[1] = new BigInteger(1+"");
for(int i=2; i<=n; i++){
b[i] = (b[i-1].multiply(new BigInteger(2+"")).mod(NUM));
}
a[1] = new BigInteger(1+"");
a[2] = new BigInteger(6+"");
for(int i=3; i<=n; i++){
temp[i] = (a[i-1].multiply(new BigInteger(2+"")).add(b[i]));
a[i] = a[i-2].multiply(new BigInteger(4+"")).add(temp[i]).mod(NUM);
}
sum = a[n].multiply(new BigInteger(4+""));
for(int i=2; i<n; i++){
temp1 = b[n-i].multiply(a[i-1].multiply(new BigInteger(8+""))).mod(NUM);
temp2 = b[i-1].multiply(a[n-i].multiply(new BigInteger(8+""))).mod(NUM);
sum = sum.add(temp1).add(temp2).mod(NUM);
}
if(n==1){
sum = new BigInteger(2+"");
}
System.out.println(sum);
}
}