結果

問題 No.320 眠れない夜に
ユーザー ぴろずぴろず
提出日時 2015-12-17 21:05:19
言語 Java19
(openjdk 21)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 1,188 bytes
コンパイル時間 2,926 ms
コンパイル使用メモリ 80,028 KB
実行使用メモリ 93,888 KB
最終ジャッジ日時 2023-10-14 12:46:31
合計ジャッジ時間 9,823 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,472 KB
testcase_01 AC 125 ms
55,564 KB
testcase_02 AC 128 ms
55,352 KB
testcase_03 AC 127 ms
55,564 KB
testcase_04 AC 129 ms
55,568 KB
testcase_05 AC 171 ms
60,736 KB
testcase_06 AC 173 ms
60,560 KB
testcase_07 AC 177 ms
60,620 KB
testcase_08 AC 128 ms
55,556 KB
testcase_09 AC 125 ms
55,652 KB
testcase_10 AC 167 ms
58,400 KB
testcase_11 AC 131 ms
55,616 KB
testcase_12 AC 162 ms
58,128 KB
testcase_13 AC 138 ms
55,652 KB
testcase_14 AC 415 ms
93,888 KB
testcase_15 AC 125 ms
55,652 KB
testcase_16 AC 126 ms
55,804 KB
testcase_17 AC 126 ms
55,672 KB
testcase_18 AC 126 ms
55,920 KB
testcase_19 AC 126 ms
55,400 KB
testcase_20 AC 137 ms
57,888 KB
testcase_21 AC 126 ms
55,644 KB
testcase_22 AC 124 ms
55,608 KB
testcase_23 AC 126 ms
55,480 KB
testcase_24 AC 126 ms
55,448 KB
testcase_25 AC 126 ms
55,784 KB
testcase_26 AC 130 ms
55,484 KB
testcase_27 AC 127 ms
55,584 KB
testcase_28 AC 127 ms
55,756 KB
testcase_29 AC 126 ms
55,604 KB
testcase_30 AC 128 ms
55,984 KB
testcase_31 AC 127 ms
55,820 KB
testcase_32 AC 125 ms
55,576 KB
testcase_33 AC 242 ms
70,240 KB
testcase_34 AC 125 ms
55,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no320;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		long[] fib = new long[81];
		long[] fibsum = new long[81];
		fib[1] = fib[2] = 1;
		for(int i=3;i<=80;i++) {
			fib[i] = fib[i-2] + fib[i-1];
		}
		for(int i=1;i<=80;i++) {
			fibsum[i] = fibsum[i-1] + fib[i];
		}
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long m = sc.nextLong();
		long x = fib[n] - m;
		if (x == 0) {
			System.out.println(0);
			return;
		}
		
		//フィボナッチ数列は指数的に増加するから大きい方から詰めていけば実はOKという予想
		ArrayList<Pair> al = new ArrayList<>();
		al.add(new Pair(0,0));
		for(int i=n-2;i>=1;i--) {
			ArrayList<Pair> next = new ArrayList<>();
			for(Pair p:al) {
				if (p.x + fib[i] == x) {
					System.out.println(p.dist + 1);
					return;
				}else if(p.x + fib[i] < x) {
					next.add(new Pair(p.x + fib[i], p.dist + 1));
				}
				if (p.x + fibsum[i-1] >= x) {
					next.add(p);
				}
			}
			al = next;
		}
		System.out.println(-1);
	}
}
class Pair {
	int dist;
	long x;
	public Pair(long x,int dist) {
		this.x = x;
		this.dist = dist;
	}
}
0