結果

問題 No.117 組み合わせの数
ユーザー te-shte-sh
提出日時 2017-02-01 11:39:05
言語 D
(dmd 2.106.1)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,357 bytes
コンパイル時間 869 ms
コンパイル使用メモリ 255,444 KB
最終ジャッジ日時 2024-04-27 02:24:11
合計ジャッジ時間 1,188 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、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, char)`
Main.d(47):        instantiated from here: `writeln!(FactorRing!1000000007)`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.regex;     // RegEx

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

void main()
{
  auto bufFrac = new mint[](2_000_000), sf = 1;
  bufFrac[0] = 1;

  auto frac(int n) {
    if (sf <= n) {
      foreach (i; sf..n+1)
        bufFrac[i] = bufFrac[i - 1] * i;
      sf = n + 1;
    }
    return bufFrac[n];
  }

  auto t = readln.chomp.to!size_t;
  foreach (_; t.iota) {
    auto rd = readln.chomp;
    auto m = rd.matchFirst(r"([CPH])\((\d+),(\d+)\)");
    auto n = m[2].to!int, k = m[3].to!int;

    auto r = mint(0);
    switch (m[1]) {
    case "C":
      if (n >= k)
        r = frac(n) * frac(k).inv * frac(n - k).inv;
      break;
    case "P":
      if (n >= k)
        r = frac(n) * frac(n - k).inv;
      break;
    case "H":
      if (n > 0)
        r = frac(n + k - 1) * frac(k).inv * frac(n - 1).inv;
      else if (k == 0)
        r = 1;
      break;
    default:
      assert(0);
    }

    writeln(r);
  }
}

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

  auto mod(long _v) {
    if (_v > 0) return _v % m;
    else return ((_v % m) + m) % m;
  }

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

  auto opBinary(string op)(int rhs)
    if (op == "+" || op == "-" || op == "*") { return opBinary!op(FactorRing!m(rhs)); }
  auto opBinary(string op: "^^")(int rhs) { return pow(this, rhs); }

  auto pow(FactorRing!m a, int b) {
    if (b == 0) return FactorRing!m(1);
    if (a == 0) return FactorRing!m(0);
    auto c = FactorRing!m(1);
    for (; b > 0; a = a * a, b >>= 1)
      if ((b & 1) == 1) c = c * a;
    return c;
  }

  auto inv() {
    int x = v.to!int, a, b;
    exEuclid(x, m, a, b);
    return FactorRing!m(a);
  }
}

T exEuclid(T)(T a, T b, ref T x, ref T y)
{
  auto g = a;
  x = 1;
  y = 0;
  if (b != 0) {
    g = exEuclid(b, a % b, y, x);
    y -= a / b * x;
  }
  return g;
}
0