nyoj 45-棋盘覆盖 (高精度, Java)

棋盘覆盖

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述

在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的2×2方格(图2为其中缺右下角的一个),去覆盖2k×2k未被覆盖过的方格,求需要类似图2方格总的个数s。如k=1时,s=1;k=2时,s=5

                                                                                

图1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                             图2                     

 

 

 

 
 
输入
第一行m表示有m组测试数据;
每一组测试数据的第一行有一个整数数k;
输出
输出所需个数s;
样例输入
3
1
2
3
样例输出
1
5
21

分析:
  1、其实不管如何我们都能够在图(一)上面用图(二)铺满,并且不重复
  2、所以这道题就变成了一个大数的计算问题(直接上Java-BigInteger)

核心代码:
1 b.subtract(c).divide(d); // (b-1)/3

Java代码实现(AC):

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main{
 5     public static void main(String agrs[]){
 6         Scanner scan = new Scanner(System.in);
 7         int t = scan.nextInt();
 8         while(t != 0){
 9             t = t - 1;
10             int a = scan.nextInt();
11             BigInteger b, c, d;
12             b = new BigInteger("4");
13             c = new BigInteger("1");
14             d = new BigInteger("3");
15             b = b.pow(a);
16             System.out.println(b.subtract(c).divide(d));
17         }
18     }
19 }

PS:Python在处理过多的大数运算时会产生较大误差

Python(WA):

1 import math
2 a = (int)(input())
3 while a:
4     a = a - 1
5     b = (int)(input())
6     c = math.pow(2, b) * math.pow(2, b)
7     print((int)((c - 1) / 3)) 

猜你喜欢

转载自www.cnblogs.com/GetcharZp/p/9101815.html