結果
問題 | No.720 行列のできるフィボナッチ数列道場 (2) |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-01-27 17:27:31 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 1 ms / 2,000 ms |
コード長 | 3,137 bytes |
コンパイル時間 | 783 ms |
コンパイル使用メモリ | 120,780 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-13 03:46:42 |
合計ジャッジ時間 | 1,601 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,948 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,940 KB |
testcase_14 | AC | 1 ms
6,944 KB |
testcase_15 | AC | 1 ms
6,940 KB |
testcase_16 | AC | 1 ms
6,944 KB |
testcase_17 | AC | 1 ms
6,940 KB |
testcase_18 | AC | 1 ms
6,940 KB |
testcase_19 | AC | 1 ms
6,940 KB |
testcase_20 | AC | 1 ms
6,940 KB |
testcase_21 | AC | 1 ms
6,940 KB |
testcase_22 | AC | 1 ms
6,944 KB |
ソースコード
import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } // a^-1 (mod m) long modInv(long a, long m) in { assert(m > 0, "modInv: m > 0 must hold"); } do { long b = m, x = 1, y = 0, t; for (; ; ) { t = a / b; a -= t * b; if (a == 0) { assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1"); if (b == -1) { y = -y; } return (y < 0) ? (y + m) : y; } x -= t * y; t = b / a; b -= t * a; if (b == 0) { assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1"); if (a == -1) { x = -x; } return (x < 0) ? (x + m) : x; } y -= t * x; } } /* P = 10^9 + 7 (5 / P) = (P / 5) = (2 / 5) = -1 r = T, s = 1 - T in F_P[T] / (T^2 - T - 1) f(k) = (r^k - s^k) / (r - s) f(m) + f(2 m) + ... + f(n m) = (r^m (r^(n m) - 1) / (r^m - 1) - s^m (s^(n m) - 1) / (s^m - 1)) / (r - s) (a + b T)^-1 = (-(a + b) / (b^2 - a b - a^2), b / (b^2 - a b - a^2)) */ enum MO = 10L^^9 + 7; long[2] sub(in long[2] a, in long[2] b) { return [(a[0] - b[0]) % MO, (a[1] - b[1]) % MO]; } long[2] mul(in long[2] a, in long[2] b) { return [(a[0] * b[0] + a[1] * b[1]) % MO, (a[0] * b[1] + a[1] * b[0] + a[1] * b[1]) % MO]; } long[2] inv(in long[2] a) { const c = modInv(a[1]^^2 - a[0] * a[1] - a[0]^^2, MO); return [(-(a[0] + a[1]) * c) % MO, (a[1] * c) % MO]; } long[2] power(in long[2] a, long e) { long[2] b = a.dup, c = [1, 0]; for (; e; e >>= 1) { if (e & 1) { c = mul(c, b); } b = mul(b, b); } return c; } void main() { try { for (; ; ) { const N = readLong(); const M = readLong(); const long[2] one = [1L, 0L], r = [0L, 1L], s = [1L, -1L]; const rm = power(r, M), sm = power(s, M); const rnm = power(r, N * M), snm = power(s, N * M); const ans = mul(sub( mul(mul(rm, sub(rnm, one)), inv(sub(rm, one))), mul(mul(sm, sub(snm, one)), inv(sub(sm, one))) ), inv(sub(r, s))); assert(ans[1] == 0); writeln((ans[0] % MO + MO) % MO); } } catch (EOFException e) { } }