历届试题 PREV-15 格子刷油漆

问题描述
  X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形(如下图所示),现需要把这些格子刷上保护漆。


  你可以从任意一个格子刷起,刷完一格,可以移动到和它相邻的格子(对角相邻也算数),但不能移动到较远的格子(因为油漆未干不能踩!)
  比如: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);
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值