結果

問題 No.303 割れません
ユーザー te-shte-sh
提出日時 2017-06-15 14:29:03
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 262 ms / 10,000 ms
コード長 2,923 bytes
コンパイル時間 3,000 ms
コンパイル使用メモリ 84,704 KB
実行使用メモリ 22,968 KB
最終ジャッジ日時 2023-09-03 14:23:28
合計ジャッジ時間 4,372 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 121 ms
12,996 KB
testcase_04 AC 259 ms
21,572 KB
testcase_05 AC 258 ms
21,472 KB
testcase_06 AC 257 ms
22,968 KB
testcase_07 AC 219 ms
18,272 KB
testcase_08 AC 228 ms
18,928 KB
testcase_09 AC 227 ms
18,916 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 262 ms
21,656 KB
testcase_12 AC 231 ms
18,944 KB
testcase_13 AC 160 ms
15,960 KB
testcase_14 AC 85 ms
10,160 KB
testcase_15 AC 62 ms
8,748 KB
testcase_16 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

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);
      r -= s * s;
    }
    writeln(r.toString);
  }
}

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

  auto b = repeatedSquare!(GmpInt[][], matMul)(a, l-2, im);
  return b[0][0] + b[0][1];
}

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;
}

struct GmpInt
{
  __mpz_struct z;

  this(long v)
  {
    __gmpz_init(&z);
    __gmpz_set_si(&z, v);
  }

  auto toString()
  {
    auto sz = __gmpz_sizeinbase(&z, 10);
    auto str = new string(sz + 1);
    __gmpz_get_str(cast(char*)str.ptr, 10, &z);
    return str.ptr.fromStringz;
  }

  auto opBinary(string op)(GmpInt v)
    if (op == "+" || op == "-" || op == "*")
  {
    auto r = GmpInt(0);
    static if (op == "+") __gmpz_add(&r.z, &z, &v.z);
    else   if (op == "-") __gmpz_sub(&r.z, &z, &v.z);
    else   if (op == "*") __gmpz_mul(&r.z, &z, &v.z);
    return r;
  }

  auto opOpAssign(string op)(GmpInt v)
    if (op == "+" || op == "-" || op == "*")
  {
    static if (op == "+") __gmpz_add(&z, &z, &v.z);
    else   if (op == "-") __gmpz_sub(&z, &z, &v.z);
    else   if (op == "*") __gmpz_mul(&z, &z, &v.z);
    return this;
  }
}

extern(C) pragma(inline, false)
{
  alias __mp_limb_t = ulong;

  struct __mpz_struct
  {
    int _mp_alloc;
    int _mp_size;
    __mp_limb_t* _mp_d;
  }

  alias mpz_srcptr = const(__mpz_struct)*;
  alias mpz_ptr = __mpz_struct*;

  void __gmpz_init(mpz_ptr);

  void __gmpz_set_si(mpz_ptr, long);
  int __gmpz_set_str(mpz_ptr, const char*, int);

  void __gmpz_add(mpz_ptr, mpz_srcptr, mpz_srcptr);
  void __gmpz_sub(mpz_ptr, mpz_srcptr, mpz_srcptr);
  void __gmpz_mul(mpz_ptr, mpz_srcptr, mpz_srcptr);

  size_t __gmpz_sizeinbase(mpz_srcptr, int);
  char *__gmpz_get_str(char*, int, mpz_srcptr);
}

pragma(lib, "gmp");
0