結果

問題 No.303 割れません
ユーザー te-shte-sh
提出日時 2017-06-12 17:43:53
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 1,416 bytes
コンパイル時間 2,581 ms
コンパイル使用メモリ 147,148 KB
実行使用メモリ 18,844 KB
最終ジャッジ日時 2023-09-03 14:17:46
合計ジャッジ時間 14,277 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.bigint;    // BigInt

void main()
{
  auto l = readln.chomp.to!int;

  if (l == 1) {
    writeln(1);
    writeln(1);
  } else if (l == 2) {
    writeln(3);
    writeln(1);
  } else {
    writeln(l);
    if (l % 2 == 0)
      writeln(calc(l) - calc(l/2) ^^ 2);
    else
      writeln(calc(l));
  }
}

auto calc(int l)
{
  auto o = BigInt(1), z = BigInt(0);
  auto a1 = [[o,o,o],[o,z,z],[z,z,o]];
  auto a2 = [[o,o,z],[o,z,z],[z,z,o]];
  auto a3 = matMul(a1, a2);
  auto im = [[o,z,z],[z,o,z],[z,z,o]];

  auto b = repeatedSquare!(BigInt[][], matMul)(a3, (l-2)/2, im);
  if (l%2) b = matMul(b, a1);

  return matMulVec(b, [o,o])[0];
}

T[][] matMul(T)(T[][] a, T[][] b)
{
  auto l = b.length, m = a.length, n = b[0].length;
  auto c = new T[][](m, n);
  foreach (i; 0..m)
    foreach (j; 0..n)
      foreach (k; 0..l)
        c[i][j] += a[i][k] * b[k][j];
  return c;
}

T[] matMulVec(T)(T[][] a, T[] b)
{
  auto l = b.length, m = a.length;
  auto c = new T[](m);
  foreach (i; 0..m)
    foreach (j; 0..l)
      c[i] += a[i][j] * b[j];
  return c;
}

T repeatedSquare(T, alias pred = "a * b", U)(T a, U n, T init)
{
  import std.functional;
  alias predFun = binaryFun!pred;

  if (n == 0) return init;

  auto r = init;
  while (n > 0) {
    if ((n & 1) == 1)
      r = predFun(r, a);
    a = predFun(a, a);
    n >>= 1;
  }

  return r;
}
0