概述
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(n−1)∗6−f(n−2)+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));
}
}
}
最后
以上就是超帅鞋子为你收集整理的ICPC南宁 Twice Equation( 递推+Java的全部内容,希望文章能够帮你解决ICPC南宁 Twice Equation( 递推+Java所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复