首页 » 未分类 » 正文

Twice Equation

Twice Equation

来源:https://nanti.jisuanke.com/t/19978

题目描述

For given LL, find the smallest nn no smaller than LL for which there exists an positive integer mm for which 2m(m + 1) = n(n + 1)2m(m+1)=n(n+1).

输入

This problem contains multiple test cases. The first line of a multiple input is an integer T (1 \le T < 1000)T(1≤T<1000) followed by T input lines. Each line contains an integer L (1 \le L < 10190)L(1≤L<10190).

输出

For each given LL, output the smallest nn. If available n does not exist, output -1−1.

样例

样例输入

3
1
4
21

样例输出

3
20
119

反思

代码

import java.math.*;
import java.util.*;

public class Main {

    void solve() {
        Scanner cin = new Scanner(System.in);  //输入流
        BigInteger[] arr = new BigInteger[1001];
        arr[1] = BigInteger.valueOf(3);  //第一个
        arr[2] = BigInteger.valueOf(20);  //第二个
        BigInteger xx[] = new BigInteger[2];
        xx[0] = BigInteger.valueOf(6);
        xx[1] = BigInteger.valueOf(2);
        for(int i = 3; i <= 1000; i++) {
            arr[i] = arr[i-1].multiply(xx[0]);
            arr[i] = arr[i].subtract(arr[i-2]);
            arr[i] = arr[i].add(xx[1]);
        }
        int T = cin.nextInt();
        for(int i = 1; i <= T; i++) {
            BigInteger x = cin.nextBigInteger();
            for(int j = 1; j <= 300; j++) {
                if(x.compareTo(arr[j]) < 0) {  //大则结束
                    System.out.println(arr[j]);
                    break;
                }
            }
        }
    }

    public static void main (String[] args) {
        Main work = new Main();
        work.solve();
    }
}

发表评论