hdu 6222 Heron and His Triangle

来源:互联网 发布:2017暑运大数据报告 编辑:程序博客网 时间:2024/06/06 01:18

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6222


解析:规律:a[n] = 4*a[n-1] - a[n-2]
代码:

import java.io.*;import java.math.BigInteger;import java.util.*;public class Main {    public static Scanner s = new Scanner(System.in);    public static void main(String args[]) throws Exception {        int t;        t = s.nextInt();        while(t-- != 0)        {            BigInteger x = s.nextBigInteger();            BigInteger p = new BigInteger("4"), pp = new BigInteger("2"), k = new BigInteger("4");            BigInteger now = p;            while(now.compareTo(x) < 0)            {                now = p.multiply(k).subtract(pp);                pp = p;                p = now;            }            System.out.println(now);        }    }}