結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 37zigen37zigen
提出日時 2016-05-05 02:14:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 208 ms / 5,000 ms
コード長 5,866 bytes
コンパイル時間 3,251 ms
コンパイル使用メモリ 81,580 KB
実行使用メモリ 56,172 KB
最終ジャッジ日時 2024-04-15 13:00:53
合計ジャッジ時間 9,374 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
51,340 KB
testcase_01 AC 82 ms
51,256 KB
testcase_02 AC 151 ms
53,380 KB
testcase_03 AC 100 ms
52,420 KB
testcase_04 AC 130 ms
53,352 KB
testcase_05 AC 115 ms
53,096 KB
testcase_06 AC 125 ms
53,296 KB
testcase_07 AC 138 ms
53,288 KB
testcase_08 AC 95 ms
52,416 KB
testcase_09 AC 124 ms
53,424 KB
testcase_10 AC 116 ms
53,224 KB
testcase_11 AC 117 ms
53,464 KB
testcase_12 AC 123 ms
53,240 KB
testcase_13 AC 120 ms
53,344 KB
testcase_14 AC 103 ms
52,832 KB
testcase_15 AC 146 ms
53,348 KB
testcase_16 AC 140 ms
53,356 KB
testcase_17 AC 119 ms
53,320 KB
testcase_18 AC 140 ms
53,376 KB
testcase_19 AC 154 ms
53,472 KB
testcase_20 AC 154 ms
55,296 KB
testcase_21 AC 208 ms
56,088 KB
testcase_22 AC 154 ms
55,176 KB
testcase_23 AC 145 ms
55,520 KB
testcase_24 AC 170 ms
55,968 KB
testcase_25 AC 160 ms
56,024 KB
testcase_26 AC 173 ms
56,172 KB
testcase_27 AC 134 ms
55,592 KB
testcase_28 AC 153 ms
55,864 KB
testcase_29 AC 164 ms
55,616 KB
testcase_30 AC 151 ms
53,192 KB
testcase_31 AC 96 ms
52,872 KB
testcase_32 AC 118 ms
53,332 KB
testcase_33 AC 126 ms
53,416 KB
testcase_34 AC 121 ms
53,288 KB
testcase_35 AC 119 ms
53,108 KB
testcase_36 AC 144 ms
53,280 KB
testcase_37 AC 97 ms
52,896 KB
testcase_38 AC 153 ms
53,448 KB
testcase_39 AC 117 ms
53,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
    long MOD = 1_000_000_000 + 7;

    class Node {
        long value;
        Node next;

        Node(long value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    void test1(int n, long k, long[] a) {
        Node first = null;
        Node last = null;

        Node p = null;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            Node tmp = new Node(a[i], null);
            if (first == null) {
                first = tmp;
                p = tmp;
            } else {
                p.next = tmp;
                p = tmp;
            }
            sum += a[i];
            sum %= MOD;
        }
        last = p;

        long tmp = sum;
        for (int i = n; i < k; i++) {
            Node node = new Node(tmp, null);

            sum += tmp;

            tmp -= first.value;
            first = first.next;
            last.next = node;
            last = last.next;
            tmp += last.value;

            tmp %= MOD;
            tmp += MOD;
            tmp %= MOD;
            sum %= MOD;
            sum += MOD;
            sum %= MOD;
        }
        System.out.println(last.value + " " + sum);
    }

    void test2(int n, long k, long[] a) {
        long[][] mat = new long[n + 1][n + 1];

        Arrays.fill(mat[0], 1);
        Arrays.fill(mat[1], 1);
        mat[1][0] = 0;
        for (int i = 1; i < n; i++) {
            mat[i + 1][i] = 1;
        }

        long[][] tmp = power(mat, k - n);
        long[] x = new long[n + 1];
        for (int i = 0; i < n; i++) {
            x[0] += a[i];
            x[i + 1] = a[n - i - 1];
        }
        long ans1 = 0, ans2 = 0;
        for (int i = 0; i < n + 1; i++) {
            ans2 += tmp[0][i] * x[i];
            ans1 += tmp[1][i] * x[i];
            ans1 %= MOD;
            ans2 %= MOD;
        }

        System.out.println(ans1 + " " + ans2);
    }

    long[][] power(long[][] a, long k) {
        int n = a.length;
        long[][] res = new long[n][n];
        long[][] tmp = new long[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp[i][j] = a[i][j];
            }
            res[i][i] = 1;
        }

        while (0 < k) {
            if (k % 2 == 1) {
                res = mul(res, tmp);
            }
            tmp = mul(tmp, tmp);
            k >>= 1;
        }

        return res;
    }

    long[][] mul(long[][] a, long[][] b) {
        int n = a.length;
        long[][] res = new long[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                res[i][j] = 0;
                for (int k = 0; k < n; k++) {
                    res[i][j] += a[i][k] * b[k][j];
                    res[i][j] %= MOD;
                }
            }
        }

        return res;
    }

    void run() {
        MyScanner sc = new MyScanner();

        int n = sc.nextInt();
        long k = sc.nextLong();
        long[] a = sc.nextLongArray(n);

        if (n <= 10000 && k <= 1000000) {
            test1(n, k, a);
        } else {
            test2(n, k, a);
        }
    }

    public static void main(String[] args) {
        new Main().run();
    }

    public void mapDebug(int[][] a) {
        System.out.println("--------map display---------");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.printf("%3d ", a[i][j]);
            }
            System.out.println();
        }
        System.out.println("----------------------------" + '\n');
    }

    class MyScanner {
        int read() {
            try {
                return System.in.read();
            } catch (IOException e) {
                throw new InputMismatchException();
            }
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        boolean isEndline(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

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

        int[] nextIntArray(int n) {
            int[] array = new int[n];
            for (int i = 0; i < n; i++)
                array[i] = nextInt();
            return array;
        }

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

        long[] nextLongArray(int n) {
            long[] array = new long[n];
            for (int i = 0; i < n; i++)
                array[i] = nextLong();
            return array;
        }

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

        double[] nextDoubleArray(int n) {
            double[] array = new double[n];
            for (int i = 0; i < n; i++)
                array[i] = nextDouble();
            return array;
        }

        String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        String[] nextStringArray(int n) {
            String[] array = new String[n];
            for (int i = 0; i < n; i++)
                array[i] = next();

            return array;
        }

        String nextLine() {
            int c = read();
            while (isEndline(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndline(c));
            return res.toString();
        }
    }
}
0