結果

問題 No.42 貯金箱の溜息
ユーザー te-shte-sh
提出日時 2017-08-10 17:42:41
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 3,028 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 93,452 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 15:44:00
合計ジャッジ時間 1,478 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
4,384 KB
testcase_01 AC 28 ms
4,380 KB
testcase_02 AC 28 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const mod = 10 ^^ 9 + 9;
alias FactorRing!mod mint;

const c = [1, 5, 10, 50, 100, 500], nc = c.length, dpm = 3500;
const dp = calcDp();

void main()
{
  auto t = readln.chomp.to!size_t;
  foreach (_; 0..t) {
    auto m = readln.chomp.to!long;
    writeln(calc(m));
  }
}

auto calc(long m)
{
  if (m <= dpm) return dp[m];

  auto md = mint(m / 500), mm = m % 500;

  auto r = mint(0);
  foreach (i; 0..nc+1) {
    auto s = mint(dp[mm + i * 500]);
    foreach (j; 0..nc+1)
      if (i != j) {
        s *= mint(md - j);
        s *= mint(i - j).inv;
      }
    r += s;
  }

  return r;
}

auto calcDp()
{
  auto dp1 = new int[](dpm+1), dp2 = new int[](dpm+1);
  dp1[] = 1;
  foreach (k; 1..nc) {
    dp2[] = dp1[];
    foreach (i; c[k]..dpm+1)
      dp2[i] += dp2[i-c[k]];
    dp1[] = dp2[];
  }
  return dp1;
}

struct FactorRing(int m, bool pos = false)
{
  version(BigEndian) {
    union { long vl; struct { int vi2; int vi; } }
  } else {
    union { long vl; int vi; }
  }

  @property int toInt() { return vi; }
  alias toInt this;

  this(int v) { vi = v; }
  this(int v, bool runMod) { vi = runMod ? mod(v) : v; }
  this(long v) { vi = mod(v); }

  ref FactorRing!(m, pos) opAssign(int v) { vi = v; return this; }

  pure auto mod(int v) const
  {
    static if (pos) return v % m;
    else return (v % m + m) % m;
  }

  pure auto mod(long v) const
  {
    static if (pos) return cast(int)(v % m);
    else return cast(int)((v % m + m) % m);
  }

  static if (m < int.max / 2) {
    pure auto opBinary(string op: "+")(int rhs) const { return FactorRing!(m, pos)(mod(vi + rhs)); }
    pure auto opBinary(string op: "-")(int rhs) const { return FactorRing!(m, pos)(mod(vi - rhs)); }
  } else {
    pure auto opBinary(string op: "+")(int rhs) const { return FactorRing!(m, pos)(mod(vl + rhs)); }
    pure auto opBinary(string op: "-")(int rhs) const { return FactorRing!(m, pos)(mod(vl - rhs)); }
  }
  pure auto opBinary(string op: "*")(int rhs) const { return FactorRing!(m, pos)(mod(vl * rhs)); }

  pure auto opBinary(string op)(FactorRing!(m, pos) rhs) const
    if (op == "+" || op == "-" || op == "*") { return opBinary!op(rhs.vi); }

  static if (m < int.max / 2) {
    auto opOpAssign(string op: "+")(int rhs) { vi = mod(vi + rhs); }
    auto opOpAssign(string op: "-")(int rhs) { vi = mod(vi - rhs); }
  } else {
    auto opOpAssign(string op: "+")(int rhs) { vi = mod(vl + rhs); }
    auto opOpAssign(string op: "-")(int rhs) { vi = mod(vl - rhs); }
  }
  auto opOpAssign(string op: "*")(int rhs) { vi = mod(vl * rhs); }

  auto opOpAssign(string op)(FactorRing!(m, pos) rhs)
    if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(rhs.vi); }

  pure auto inv() const
  {
    int x = vi, a, b;
    exEuclid(x, m, a, b);
    return FactorRing!(m, pos)(a);
  }
}

pure 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