HDU 5351 MZL's Border(2015多校第五場第9題) 寫長串找規律

題目鏈接:
http://acm.hdu.edu.cn/showproblem.php?pid=5351

題意:求Fibn的前m位的LBorder

思路:一開始完全沒頭緒,就開始一個個的寫出來,最終就找出了規律,由於數據會很大用JAVA的大數會比較方便

代碼:

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

public class Main {
	public static void main(String[] args) {
		int T, n, i;
		BigInteger m, mod, mm, ans = null;
		mod = BigInteger.valueOf(258280327);
		BigInteger[] a = new BigInteger[1005];
		a[1] = BigInteger.ONE;
		a[2] = a[1];
		for (i = 3; i < 1005; ++i) {
			a[i] = a[i - 1].add(a[i - 2]);
		}
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			T = cin.nextInt();
			while (T > 0) {
				T--;
				n = cin.nextInt();
				m = cin.nextBigInteger();
				mm = m.add(BigInteger.ONE);
				for (i = 1; i < 1005; i++) {
					if (mm.compareTo(a[i]) < 0) {
						ans = m.subtract(a[i - 2]).mod(mod);
						break;
					}
				}
				System.out.println(ans);
			}
		}
		cin.close();
	}
}


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