我是靠谱客的博主 超帅鞋子,这篇文章主要介绍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.

样例

复制代码
1
2
3
4
5
6
7
8
9
10
11
样例输入 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代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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(); } }

大数类的基本运算法则

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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南宁内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部