結果

問題 No.140 みんなで旅行
ユーザー te-shte-sh
提出日時 2016-09-14 16:17:32
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 1,584 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 104,056 KB
実行使用メモリ 5,920 KB
最終ジャッジ日時 2023-09-02 22:08:48
合計ジャッジ時間 2,439 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 3 ms
4,368 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 1 ms
4,368 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 1 ms
4,368 KB
testcase_10 AC 1 ms
4,372 KB
testcase_11 AC 51 ms
5,688 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,368 KB
testcase_14 AC 50 ms
5,616 KB
testcase_15 AC 50 ms
5,920 KB
testcase_16 AC 18 ms
4,368 KB
testcase_17 AC 9 ms
4,372 KB
testcase_18 AC 36 ms
4,412 KB
testcase_19 AC 44 ms
5,684 KB
testcase_20 AC 7 ms
4,368 KB
testcase_21 AC 1 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

const int mod = 10 ^^ 9 + 7;

void main()
{
  auto n = readln.chomp.to!int;

  auto p = pascalTriangle!int(n, mod);

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

  int calc(int x, int y) {
    if (memo[x][y] >= 0) {
      return memo[x][y];
    } else {
      auto r1 = modMul(calc(x - 1, y), y, mod);
      auto r2 = (calc(x - 1, y - 1) + r1) % mod;
      return memo[x][y] = r2;
    }
  }

  auto r = 0;
  foreach (i; 1..n+1) {
    foreach (j; i..n+1) {
      auto s1 = p[n][j];
      auto s2 = calc(j, i);
      auto s3 = modMul(s1, s2, mod);
      auto s4 = modPow(i * (i - 1), n - j, mod);
      r = (r + modMul(s3, s4, mod)) % mod;
    }
  }

  writeln(r);
}

int modMul(int a, int b, int mod)
{
  return ((a.to!long * b.to!long) % mod).to!int;
}

T modPow(T)(T a, T b, T mod)
{
  if (b == 0) return 1;
  if (a == 0) return 0;
  T c = 1;
  for (; b > 0; a = modMul(a, a, mod), b >>= 1)
    if ((b & 1) == 1) c = modMul(c, a, mod);
  return c;
}

T[][] pascalTriangle(T)(size_t n, T mod = 0)
{
  auto t = new T[][](n + 1);
  t[0] = new T[](1);
  t[0][0] = 1;
  foreach (i; 1..n+1) {
    t[i] = new T[](i + 1);
    t[i][0] = t[i][$-1] = 1;
    foreach (j; 1..i) {
      t[i][j] = t[i - 1][j - 1] + t[i - 1][j];
      if (mod != 0) t[i][j] %= mod;
    }
  }
  return t;
}
0