大数 线性递推 Twice Equation

题目链接:https://nanti.jisuanke.com/t/A1541

题意:给你一个L,求不小于L的最小的N,使的存在正整数m满足2m(m+1)=n(n+1)

分析:这题一看就没什么思路,就索性直接根据2m(m+1)=n(n+1)打表,来找规律,可以发现前面几项就是3,20,119,696,4059

尝试一下找规律,如果自己不会找就直接用求线性递推式系数公式来暴力枚举,成功得到递推公式是f(n)=6f(n-1)-f(n-2)+2;

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

public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        int T=cin.nextInt();
        BigInteger[] fn=new BigInteger [1500];
        BigInteger two=new BigInteger("2");
        BigInteger six=new BigInteger("6");
        BigInteger L;
        fn[1]=new BigInteger("3");fn[2]=new BigInteger("20");
        for(int i=3;i<=1200;i++) {
            fn[i]=fn[i-1].multiply(six).subtract(fn[i-2]).add(two);
        }
        for(int i=0;i<T;i++) {
            L=cin.nextBigInteger();
            for(int j=1;j<=1200;j++) {
                if(L.compareTo(fn[j])<=0) {
                    System.out.println(fn[j]);
                    break;
                }
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/qingjiuling/p/11360168.html