結果
| 問題 |
No.1704 Many Bus Stops (easy)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-10-08 22:54:27 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 250 ms / 2,000 ms |
| コード長 | 1,489 bytes |
| コンパイル時間 | 669 ms |
| コンパイル使用メモリ | 114,152 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-22 12:45:22 |
| 合計ジャッジ時間 | 8,578 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 41 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
immutable long MOD = 10^^9 + 7;
void main() {
auto T = readln.chomp.to!int;
long a = powmod(3, MOD-2, MOD);
long b = 1L;
long[][] X = [[a, 0, 0, a, 0, a],
[0, a, 0, a, a, 0],
[0, 0, a, 0, a, a],
[0, 0, b, 0, 0, 0],
[b, 0, 0, 0, 0, 0],
[0, b, 0, 0, 0, 0]];
while (T--) {
auto N = readln.chomp.to!int;
auto ans = matpow(6, N, X);
ans[0][0].writeln;
}
}
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
long[][] matmul(int N, long[][] m1, long[][] m2) {
auto ret = new long[][](N, N);
foreach (i; 0..N) {
foreach (j; 0..N) {
ret[i][j] = 0;
foreach (k; 0..N) {
(ret[i][j] += m1[i][k] * m2[k][j] % MOD) %= MOD;
}
}
}
return ret;
}
long[][] matpow(int N, long K, long[][] mat) {
auto ret = new long[][](N, N);
foreach (i; 0..N)
foreach (j; 0..N)
ret[i][j] = i == j ? 1 : 0;
while (K > 0) {
if (K % 2 == 1)
ret = matmul(N, ret, mat);
mat = matmul(N, mat, mat);
K /= 2;
}
return ret;
}