結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー GrenacheGrenache
提出日時 2016-06-08 22:45:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 164 ms / 5,000 ms
コード長 3,021 bytes
コンパイル時間 3,563 ms
コンパイル使用メモリ 84,628 KB
実行使用メモリ 51,936 KB
最終ジャッジ日時 2024-10-09 04:54:26
合計ジャッジ時間 10,091 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
41,300 KB
testcase_01 AC 99 ms
40,788 KB
testcase_02 AC 141 ms
41,276 KB
testcase_03 AC 108 ms
40,776 KB
testcase_04 AC 131 ms
41,412 KB
testcase_05 AC 130 ms
41,296 KB
testcase_06 AC 131 ms
41,512 KB
testcase_07 AC 135 ms
41,280 KB
testcase_08 AC 99 ms
40,148 KB
testcase_09 AC 132 ms
41,276 KB
testcase_10 AC 114 ms
40,996 KB
testcase_11 AC 119 ms
41,104 KB
testcase_12 AC 127 ms
41,196 KB
testcase_13 AC 115 ms
41,152 KB
testcase_14 AC 94 ms
39,744 KB
testcase_15 AC 143 ms
41,412 KB
testcase_16 AC 141 ms
41,452 KB
testcase_17 AC 114 ms
41,556 KB
testcase_18 AC 149 ms
41,644 KB
testcase_19 AC 142 ms
41,260 KB
testcase_20 AC 135 ms
48,784 KB
testcase_21 AC 164 ms
49,892 KB
testcase_22 AC 136 ms
49,072 KB
testcase_23 AC 127 ms
42,292 KB
testcase_24 AC 148 ms
45,672 KB
testcase_25 AC 145 ms
48,096 KB
testcase_26 AC 154 ms
48,200 KB
testcase_27 AC 132 ms
45,864 KB
testcase_28 AC 134 ms
42,668 KB
testcase_29 AC 147 ms
51,936 KB
testcase_30 AC 137 ms
41,428 KB
testcase_31 AC 103 ms
41,060 KB
testcase_32 AC 126 ms
41,680 KB
testcase_33 AC 135 ms
41,312 KB
testcase_34 AC 135 ms
41,416 KB
testcase_35 AC 133 ms
41,556 KB
testcase_36 AC 135 ms
41,220 KB
testcase_37 AC 111 ms
40,812 KB
testcase_38 AC 142 ms
41,416 KB
testcase_39 AC 129 ms
41,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder194 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int n = sc.nextInt();
        long k = sc.nextLong();

        if (k <= 1_000_000) {
        	int[] f = new int[(int)k + 1];
        	int[] s = new int[(int)k + 1];

        	for (int i = 1; i <= k; i++) {
        		if (i <= n) {
        			f[i] = sc.nextInt();
        		} else {
        			f[i] = (s[i - 1] - s[i - n - 1] + MOD) % MOD;
        		}

        		s[i] = (s[i - 1] + f[i]) % MOD;
        	}

        	pr.printf("%d %d\n", f[(int)k], s[(int)k]);
        } else {
    		int[] a = new int[n + 1];
    		for (int i = 0; i < n; i++) {
    			a[i] = sc.nextInt();
    			a[n] += a[i];
    		}
    		int[][] x = new int[n + 1][n + 1];
    		for (int i = 0; i < n - 1; i++) {
    			x[i][i + 1] = 1;
    		}
    		Arrays.fill(x[n - 1], 1);
    		x[n - 1][n] = 0;
    		Arrays.fill(x[n], 1);

    		long loop = k - n;
    		while (loop > 0) {
    			if (loop % 2 == 1) {
    				a = Matrix.mul(x, a);
    			}
    			x = Matrix.mul(x, x);
    			loop /= 2;
    		}

        	pr.printf("%d %d\n", a[n - 1], a[n]);
        }

        pr.close();
        sc.close();
    }

    private static final int MOD = 1_000_000_007;

	private static class Matrix {
		static int[] mul(int[][] x, int[] a) {
			int n = a.length;
			int[] b = new int[n];
			for (int i = 0; i < n; i++) {
				long tmp = 0;
				for (int j = 0; j < n; j++) {
					tmp += (long)x[i][j] * a[j];
					tmp %= MOD;
				}
				b[i] = (int)tmp;
			}

			return b;
		}

		static int[][] mul(int[][] x, int[][] x2) {
			int n = x[0].length;
			int[][] b = new int[n][n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					long tmp = 0;
					for (int k = 0; k < n; k++) {
						tmp += (long)x[i][k] * x2[k][j];
						tmp %= MOD;
					}
					b[i][j] = (int)tmp;
				}
			}

			return b;
		}
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0