5210: Twice Equation
时间限制: 1 Sec 内存限制: 128 MB提交: 113 解决: 46
[提交][状态][讨论版][命题人:admin]
题目描述
For given L, find the smallest n no smaller than L for which there exists an positive integer m for which 2m(m + 1) = n(n + 1).
输入
This problem contains multiple test cases. The first line of a multiple input is an integer T (1 ≤ T < 1000) followed
by T input lines. Each line contains an integer L (1 ≤ L < 10190 ).
输出
For each given L, output the smallest n. If available n does not exist, output −1.
样例输入
3
1
4
21
样例输出
3
20
119
提示
通过关系 2*m*(m+1) = n*(n+1) 打表能发现关于n的递推关系式 fn[i](第i项的n值) = 6*fn[i-1] - fn[i-2] + 2 , 由于数据范围达到了惊人的 10e190 , 所以我们可以用 java 大数解决此问题,代码如下:
import java.util.Scanner ;
import java.math.BigInteger ;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
BigInteger[] fn = new BigInteger[1500] ;
BigInteger two = BigInteger.valueOf(2) ;
BigInteger six = BigInteger.valueOf(6) ;
fn[1] = BigInteger.valueOf(3) ;
fn[2] = BigInteger.valueOf(20) ;
for ( int i = 3 ; i <= 1200 ; i ++ ) {
fn[i] = fn[i-1].multiply(six).subtract(fn[i-2]).add(two) ;
}
int t = sc.nextInt() ;
while ( t -- > 0 ) {
BigInteger L = sc.nextBigInteger() ;
for ( int i = 1 ; i <= 500 ; i ++ ) {
if ( L.compareTo(fn[i]) < 0 ) {
System.out.println(fn[i]) ;
break ;
}
}
}
}
}
(做题的时候一看这题就是数学关系题 , 画出函数图像 , 列方程找关系 …… 想过打表但看到数据没找出递推关系, 懵了好久, 后来看了别人的题解才发现有这递推关系式(.........), 还是学长和同级dalao威武.... )
(膜拜dalao) o(*////▽////*)q
(补题练练 java 大数, 有些许不明白, 记录一下..)