結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
18,584 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 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("INF");
  } else {
    writeln(l);
    auto r = calc(l);
    if (l % 2 == 0) {
      auto s = calc(l/2);
      writeln(r - s * s);
    } else {
      writeln(r);
    }
  }
}

auto calc(int l)
{
  auto o = BigInt(1), z = BigInt(0);
  auto a = [[o,o],[o,z]];
  auto im = [[o,z],[z,o]];

  auto b = repeatedSquare!(BigInt[][], matMul)(a, l-2, im);
  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;

  static buf = new T[](32);
  static bufFilled = 0;

  if (bufFilled == 0)
    buf[bufFilled++] = a;

  auto r = init, i = 0;
  while (n > 0) {
    if ((n & 1) == 1)
      r = predFun(r, buf[i]);
    if (++i == bufFilled)
      buf[bufFilled++] = predFun(buf[i-1], buf[i-1]);
    n >>= 1;
  }

  return r;
}
0