ICPC Nanning L. Twice Equation (打表找規律)(level 1)

題目鏈接

題意:

2*m(m+1) = n*(n+1)

然後給你一個L,讓你輸出一個最小的n>=L,(所有數都是整數)

解析:

這道題雖然只有level 1,比賽的時候也很多人做出來,但是這種題我是完全做不出來....

m: 0  tot: 0  n:0.0
m: 2  tot: 12  n:3.0
m: 14  tot: 420  n:20.0
m: 84  tot: 14280  n:119.0
m: 492  tot: 485112  n:696.0
m: 2870  tot: 16479540  n:4059.0
m: 16730  tot: 559819260  n:23660.0
m: 97512  tot: 19017375312  n:137903.0
m: 568344  tot: 646030941360  n:803760.0
m: 3312554  tot: 21946034630940  n:4684659.0
m: 19306982  tot: 745519146510612  n:27304196.0
m: 112529340  tot: 25325704946729880  n:159140519.0
m: 655869060  tot: 860328449042305320  n:927538920.0

打了這麼多項的表也找不出任何規律..我本來以爲是用那個數學公式推出來的公式

結果...直接找規律,猜公式....我隊友找到的規律是

f[i]=6*f[i-1]-6*f[i-3]+f[i-4]

其實官方的題解的規律是f[i]=6*f[i-1]-f[i-2]+2

這個公式其實可以化成上面那個式子的



import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	
	
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		
		for(int o=0;o<t;o++){
			BigInteger f0=BigInteger.ZERO;
			BigInteger f1=BigInteger.valueOf(3);
			BigInteger f2=BigInteger.valueOf(20);
			BigInteger f3=BigInteger.valueOf(119);
			BigInteger six=BigInteger.valueOf(6);
			BigInteger L=sc.nextBigInteger();
			if(L.compareTo(f1)<=0)
			{
				System.out.println(f1);
				continue;
			}
			if(L.compareTo(f2)<=0)
			{
				System.out.println(f2);
				continue;
			}
			while(L.compareTo(f3)>0){
				BigInteger res=f3.multiply(six).subtract(f1.multiply(six)).add(f0);
				f0=f1;
				f1=f2;
				f2=f3;
				f3=res;
			}
			System.out.println(f3);
		}
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章