题目链接: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);
}
}
}
本文提供了一道 HDU 6222 的算法题解,通过发现数列规律 a[n]=4*a[n-1]-a[n-2] 并使用 BigInteger 类实现递推求解,适用于解决大规模数值计算问题。
7209

被折叠的 条评论
为什么被折叠?



