結果

問題 No.829 成長関数インフレ中
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-05-03 22:56:19
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 4,198 bytes
コンパイル時間 1,000 ms
コンパイル使用メモリ 129,828 KB
実行使用メモリ 12,988 KB
最終ジャッジ日時 2024-06-22 01:12:06
合計ジャッジ時間 3,112 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 21 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 15 ms
6,940 KB
testcase_15 AC 34 ms
6,944 KB
testcase_16 AC 67 ms
6,972 KB
testcase_17 AC 122 ms
12,988 KB
testcase_18 AC 114 ms
9,232 KB
testcase_19 AC 106 ms
9,548 KB
testcase_20 AC 135 ms
12,828 KB
testcase_21 AC 11 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b;
    a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) {
        y = -y;
      }
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a;
    b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) {
        x = -x;
      }
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}


enum MO = 10L^^9 + 7;

int N;
long B;
int[] A;

int[] solveBrute() {
  auto cnt = new int[N + 1];
  auto perm = iota(N).array;
  do {
    int g;
    int mx = -1;
    foreach (i; 0 .. N) {
      if (chmax(mx, A[perm[i]])) {
        ++g;
      }
    }
    ++cnt[g];
  } while (perm.nextPermutation);
  return cnt;
}

void main() {
  /*
  foreach (n; 1 .. 9) {
    N = n;
    A = iota(n).array;
    writeln(n, ": ", solveBrute());
  }
  
  foreach (n; 1 .. 6) {
    foreach (s; 0 .. n^^n) {
      N = n;
      A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = s / n^^i % n;
      }
      writeln("A = ", A, ": ", solveBrute());
    }
  }
  */
  
  try {
    for (; ; ) {
      N = readInt();
      B = readLong();
      A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt();
      }
      
      debug {
        writeln("A = ", A, ": ", solveBrute());
      }
      
      A.sort;
      A.reverse;
      Tuple!(long, long)[] factors;
      int cnt = 0;
      long coef = 1;
      for (int i = 0, j = 0; i < N; i = j) {
        for (; j < N && A[i] == A[j]; ++j) {}
        factors ~= tuple!(long, long)(i, j - i);
        coef *= (j - i);
        coef %= MO;
        ++cnt;
        foreach (k; i + 1 .. j) {
          coef *= cnt;
          coef %= MO;
          ++cnt;
        }
      }
      long prodMax = 1;
      foreach (f; factors) {
        prodMax *= f[1];
        prodMax %= MO;
      }
      coef *= modInv(prodMax, MO);
      coef %= MO;
      debug {
        write(coef);
        foreach (f; factors) {
          writef(" (%s + %s q)", f[0], f[1]);
        }
        writeln();
      }
      
      /*
        (d / dq) (Prod (a_i + b_i q))
        = Sum (b_i Prod[j != i] (a_j + b_j q))
      */
      int zero = 0;
      long prod = 1;
      foreach (f; factors) {
        const t = (f[0] + f[1] * B) % MO;
        if (t == 0) {
          ++zero;
        } else {
          prod *= t;
          prod %= MO;
        }
      }
      long ans;
      foreach (f; factors) {
        const t = (f[0] + f[1] * B) % MO;
        if (t == 0) {
          if (zero == 1) {
            ans += f[1] * prod;
            ans %= MO;
          }
        } else {
          if (zero == 0) {
            ans += f[1] * ((prod * modInv(t, MO)) % MO);
            ans %= MO;
          }
        }
      }
      ans *= coef;
      ans %= MO;
      ans *= B;
      ans %= MO;
      ans = (ans % MO + MO) % MO;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0