結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー te-shte-sh
提出日時 2017-04-20 22:21:33
言語 D
(dmd 2.106.1)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,042 bytes
コンパイル時間 529 ms
コンパイル使用メモリ 141,176 KB
最終ジャッジ日時 2024-11-14 20:00:04
合計ジャッジ時間 1,034 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/format/internal/write.d(143): Error: cannot implicitly convert expression `obj` of type `const(FactorRing!1000000007)` to `int`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/format/write.d(1239): Error: template instance `std.format.internal.write.formatValueImpl!(LockingTextWriter, FactorRing!1000000007, char)` error instantiating
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/format/write.d(632):        instantiated from here: `formatValue!(LockingTextWriter, FactorRing!1000000007, char)`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/stdio.d(1759):        instantiated from here: `formattedWrite!(LockingTextWriter, char, FactorRing!1000000007)`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/stdio.d(4277):        instantiated from here: `write!(FactorRing!1000000007, string, FactorRing!1000000007, char)`
Main.d(19):        instantiated from here: `writeln!(FactorRing!1000000007, string, FactorRing!1000000007)`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags

const p = 10 ^^ 9 + 7;
alias FactorRing!p mint;

void main()
{
  auto rd = readln.split.to!(long[]), n = rd[0], k = rd[1];
  auto ai = readln.split.to!(int[]).map!(to!mint).array;

  Tuple!(mint, mint) r;

  if (k <= 10 ^^ 6)
    r = calc1(n, k, ai);
  else
    r = calc2(n, k, ai);

  writeln(r[0], " ", r[1]);
}

auto calc1(long n, long k, mint[] ai)
{
  auto fi = new mint[](k);
  auto si = new mint[](k);

  fi[0] = ai[0];
  si[0] = ai[0];

  foreach (i; 1..n) {
    fi[i] = ai[i];
    si[i] = si[i - 1] + fi[i];
  }

  fi[n] = si[n - 1];
  si[n] = si[n - 1] + fi[n];

  foreach (i; n+1..k) {
    fi[i] = fi[i - 1] * 2 - fi[i - n - 1];
    si[i] = si[i - 1] + fi[i];
  }

  return Tuple!(mint, mint)(fi[$-1], si[$-1]);
}

auto calc2(long n, long k, mint[] ai)
{
  auto bi = ai.dup;
  bi.reverse();

  auto si = new mint[](n);
  si[0] = ai[0];
  foreach (i; 1..n)
    si[i] = si[i - 1] + ai[i];
  si.reverse();

  auto fm = new mint[][](n, n);
  fm[0][] = mint(1);
  foreach (i; 0..n-1) fm[i + 1][i] = mint(1);

  fm = pow(fm, k - n, n);
  auto f = zip(fm[0], bi).map!"a[0] * a[1]".fold!"a + b";

  auto sm = new mint[][](n + 1, n + 1);
  sm[0][0] = mint(2);
  sm[0][$-1] = mint(-1);
  foreach (i; 0..n) sm[i + 1][i] = mint(1);

  sm = pow(sm, k - n, n + 1);
  auto s = zip(sm[0], si).map!"a[0] * a[1]".fold!"a + b";

  return Tuple!(mint, mint)(f, s);
}

auto pow(T)(T[][] ai, long b, long n)
{
  auto m = b.bsr + 1;

  auto tbl = new T[][][](m, n, n);
  foreach (i; 0..n)
    tbl[0][i][] = ai[i];
  foreach (i; 1..m)
    tbl[i] = mul(tbl[i - 1], tbl[i - 1], n);

  auto r = new T[][](n, n);
  foreach (i; 0..n)
    r[i][i] = T(1);
  foreach (i; 0..m)
    if (b.bitTest(i))
      r = mul(r, tbl[i], n);

  return r;
}

auto mul(T)(T[][] ai, T[][] bi, long n)
{
  auto ci = new T[][](n, n);
  foreach (i; 0..n)
    foreach (j; 0..n)
      foreach (k; 0..n)
        ci[i][j] = ci[i][j] + ai[i][k] * bi[k][j];
  return ci;
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }

  import core.bitop;
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
}

struct FactorRing(int m)
{
  long v;

  @property int toInt() { return v.to!int; }
  alias toInt this;

  this(T)(T _v) { v = mod(_v); }

  ref FactorRing!m opAssign(int _v)
  {
    v = mod(_v);
    return this;
  }

  pure auto mod(long _v) const { return _v > 0 ? _v % m : ((_v % m) + m) % m; }

  pure auto opBinary(string op: "+")(FactorRing!m rhs) const { return FactorRing!m(v + rhs.v); }
  pure auto opBinary(string op: "-")(FactorRing!m rhs) const { return FactorRing!m(v - rhs.v); }
  pure auto opBinary(string op: "*")(FactorRing!m rhs) const { return FactorRing!m(v * rhs.v); }
  pure auto opBinary(string op: "^^")(FactorRing!m rhs) const { return pow(this, rhs.toInt); }

  pure auto opBinary(string op)(int rhs) const
    if (op == "+" || op == "-" || op == "*") { return opBinary!op(FactorRing!m(rhs)); }
}
0