题目链接:Twice Equation

比赛链接:ICPC Asia Nanning 2017

Description

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)\).

Input

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

Output

For each given \(L\), output the smallest \(n\). If available nn does not exist, output \(−1\).

Sample Input

3
1
4
21

Sample Output

3
20
119

Solution

题意

给出一个整数 \(L\),求大于等于 \(L\) 的最小整数 \(n\) 满足存在一个整数 \(m\) 使得 \(2m(m + 1) = n(n + 1)\)。

题解

打表找规律

\[f(n) = f(n - 1) * 6 - f(n - 2) + 2
\]

然后用 Java 大数求解即可。

Code

import java.util.Scanner;
import java.math.BigInteger; public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger[] a = new BigInteger[1000];
// 打表
a[0] = BigInteger.ZERO;
a[1] = BigInteger.valueOf(3);
BigInteger six = new BigInteger("6");
BigInteger two = new BigInteger("2");
for(int i = 2; i < 300; ++i) {
a[i] = ((a[i - 1].multiply(six)).subtract(a[i - 2])).add(two);
} int t = in.nextInt();
while (t-->0){
boolean flag = false;
BigInteger l = in.nextBigInteger();
for(int i = 0; i < 1000; ++i) {
if(a[i].compareTo(l) >= 0) {
System.out.println(a[i]);
flag = true;
break;
}
}
if(!flag) System.out.println(-1);
}
}
}

最新文章

  1. python regrex
  2. 静态时序分析(static timing analysis)
  3. 无法Ping通windows 7主机
  4. INPUT输入框灰体提示
  5. python 中参数*args, **kwargs
  6. cordova热更新
  7. javascript笔记4之运算符
  8. angular编写表单验证
  9. Java基础_0306:数组的定义与使用
  10. (15)线程---Condition条件
  11. 重定向,/dev/null, 1&gt;, 2&gt;什么意思?
  12. Easyui的datagrid的editor(行编辑器)如何扩展datetimebox类型
  13. linux查看RAID信息
  14. ShowDoc
  15. System Generator简介
  16. 如何利用Xshell在Linux下安装jdk
  17. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)
  18. windows下的DeepLearning环境搭建:Theano的安装
  19. 【大数据系统架构师】0.2 Linux基础
  20. 用intellij idea 写第一个Java程序

热门文章

  1. shell从字符串中提取子串(正则表达式)
  2. ASP.NET Core学习——7
  3. 十二、SpringBoot 优雅的集成Spring Security
  4. java 重新学习 (五)
  5. 深入JAVA虚拟机笔记
  6. neo4j简单学习
  7. java final关键字详解
  8. jQuery部分疑问及小结
  9. 负载均衡实现故障vip自动漂移
  10. win10居然把Linux的引导覆盖了