結果

問題 No.492 IOI数列
ユーザー zimphazimpha
提出日時 2017-03-29 22:00:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 49 ms / 1,000 ms
コード長 3,244 bytes
コンパイル時間 2,071 ms
コンパイル使用メモリ 74,528 KB
実行使用メモリ 50,952 KB
最終ジャッジ日時 2023-09-20 20:20:24
合計ジャッジ時間 4,054 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,888 KB
testcase_01 AC 49 ms
50,612 KB
testcase_02 AC 49 ms
50,384 KB
testcase_03 AC 47 ms
50,920 KB
testcase_04 AC 47 ms
50,440 KB
testcase_05 AC 45 ms
50,244 KB
testcase_06 AC 47 ms
50,952 KB
testcase_07 AC 47 ms
50,784 KB
testcase_08 AC 48 ms
49,888 KB
testcase_09 AC 47 ms
50,808 KB
testcase_10 AC 43 ms
50,036 KB
testcase_11 AC 43 ms
49,580 KB
testcase_12 AC 43 ms
49,760 KB
testcase_13 AC 43 ms
49,664 KB
testcase_14 AC 42 ms
49,944 KB
testcase_15 AC 42 ms
49,596 KB
testcase_16 AC 42 ms
49,836 KB
testcase_17 AC 43 ms
49,656 KB
testcase_18 AC 44 ms
49,620 KB
testcase_19 AC 43 ms
49,592 KB
testcase_20 AC 44 ms
49,752 KB
testcase_21 AC 43 ms
49,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Chiaki.Hoshinomori
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task492 solver = new Task492();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task492 {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextLong();
            out.println(solve(n - 1, BigInteger.valueOf(1000000007)));
            out.println(solve(n - 1, new BigInteger("101010101010101010101")));
        }

        BigInteger solve(long n, BigInteger mod) {
            BigInteger[] ret = new BigInteger[]{BigInteger.ONE, BigInteger.ONE};
            BigInteger[][] A = new BigInteger[2][];
            A[0] = new BigInteger[]{BigInteger.valueOf(100), BigInteger.ONE};
            A[1] = new BigInteger[]{BigInteger.ZERO, BigInteger.ONE};
            for (; n > 0; n >>= 1) {
                if (n % 2 == 1) {
                    BigInteger t0 = A[0][0].multiply(ret[0]).add(A[0][1].multiply(ret[1])).mod(mod);
                    BigInteger t1 = A[1][0].multiply(ret[0]).add(A[1][1].multiply(ret[1])).mod(mod);
                    ret[0] = t0;
                    ret[1] = t1;
                }
                A = mul(A, A, mod);
            }
            return ret[0];
        }

        private BigInteger[][] mul(BigInteger[][] a, BigInteger[][] b, BigInteger mod) {
            BigInteger[][] c = new BigInteger[2][];
            for (int i = 0; i < 2; i++) {
                c[i] = new BigInteger[2];
            }
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    c[i][j] = BigInteger.ZERO;
                    for (int k = 0; k < 2; k++) {
                        c[i][j] = c[i][j].add(a[i][k].multiply(b[k][j]));
                    }
                    c[i][j] = c[i][j].mod(mod);
                }
            }
            return c;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }
}

0