ICPC南宁 Twice Equation( 递推+Java

Twice Equation

题目描述

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

题意

推出公式 f(n)=f(n1)6f(n2)+2 f ( n ) = f ( n − 1 ) ∗ 6 − f ( n − 2 ) + 2
Java大数

AC代码

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();
    }
}

大数类的基本运算法则

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String args[]){
       Scanner cin = new Scanner(System.in);
       BigInteger a, b;

       //以文件EOF结束
       while (cin.hasNext()){
           a = cin.nextBigInteger();
           b = cin.nextBigInteger();

           System.out.println(a.add(b)); //大整数加法
           System.out.println(a.subtract(b)); //大整数减法
           System.out.println(a.multiply(b)); //大整数乘法
           System.out.println(a.divide(b)); //大整数除法(取整)
           System.out.println(a.remainder(b)); //大整数取模

           //大整数的比较
           if( a.compareTo(b) == 0 ) System.out.println("a == b"); //大整数a==b
           else if( a.compareTo(b) > 0 ) System.out.println("a > b"); //大整数a>b
           else if( a.compareTo(b) < 0 ) System.out.println("a < b"); //大整数a<b

           //大整数绝对值
           System.out.println(a.abs()); //大整数a的绝对值

           //大整数的幂
           int exponent=10;
           System.out.println(a.pow(exponent)); //大整数a的exponent次幂

           //返回大整数十进制的字符串表示
           System.out.println(a.toString());

           //返回大整数p进制的字符串表示
           int p=8;
           System.out.println(a.toString(p));
       }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值