結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー GrenacheGrenache
提出日時 2016-06-08 22:45:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 193 ms / 5,000 ms
コード長 3,021 bytes
コンパイル時間 4,579 ms
コンパイル使用メモリ 85,968 KB
実行使用メモリ 52,188 KB
最終ジャッジ日時 2024-04-17 10:58:25
合計ジャッジ時間 11,104 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
41,180 KB
testcase_01 AC 125 ms
41,052 KB
testcase_02 AC 162 ms
41,364 KB
testcase_03 AC 124 ms
41,284 KB
testcase_04 AC 146 ms
41,376 KB
testcase_05 AC 141 ms
41,680 KB
testcase_06 AC 146 ms
41,720 KB
testcase_07 AC 156 ms
41,580 KB
testcase_08 AC 120 ms
41,364 KB
testcase_09 AC 148 ms
41,796 KB
testcase_10 AC 126 ms
41,488 KB
testcase_11 AC 138 ms
41,492 KB
testcase_12 AC 144 ms
41,432 KB
testcase_13 AC 129 ms
41,520 KB
testcase_14 AC 122 ms
41,316 KB
testcase_15 AC 162 ms
41,420 KB
testcase_16 AC 155 ms
42,116 KB
testcase_17 AC 127 ms
41,268 KB
testcase_18 AC 158 ms
41,408 KB
testcase_19 AC 162 ms
41,320 KB
testcase_20 AC 158 ms
49,228 KB
testcase_21 AC 193 ms
50,272 KB
testcase_22 AC 151 ms
49,100 KB
testcase_23 AC 158 ms
42,384 KB
testcase_24 AC 173 ms
45,752 KB
testcase_25 AC 170 ms
48,672 KB
testcase_26 AC 176 ms
48,384 KB
testcase_27 AC 148 ms
45,812 KB
testcase_28 AC 154 ms
42,712 KB
testcase_29 AC 170 ms
52,188 KB
testcase_30 AC 161 ms
41,568 KB
testcase_31 AC 118 ms
41,220 KB
testcase_32 AC 140 ms
41,680 KB
testcase_33 AC 145 ms
41,500 KB
testcase_34 AC 140 ms
41,516 KB
testcase_35 AC 142 ms
41,308 KB
testcase_36 AC 157 ms
41,676 KB
testcase_37 AC 126 ms
41,412 KB
testcase_38 AC 162 ms
41,312 KB
testcase_39 AC 145 ms
41,580 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