zoukankan      html  css  js  c++  java
  • 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)) 
  • 相关阅读:
    最大流之dinic
    HDU 2485
    最小费用最大流
    HDU 1533
    HDU 1402
    HDU 1498
    HDU 1281
    Codeforces 283E Cow Tennis Tournament 线段树 (看题解)
    Codeforces 983E NN country 思维 (看题解)
    Codeforces 494D Birthday 树形dp (看题解)
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9101815.html
Copyright © 2011-2022 走看看