Submission #2374674


Source Code Expand

public class Main {

  private static void solve() {
    int n = ni();
    int d = ni();
    int[] a = na(n);

    int mod = 1000000000 + 7;
    int max = 900;

    //c[i][j] = C(D-i,j)
    long[][] c = new long[max + 1][31];
    for (int i = 0; i <= max; i ++) {
      for (int j = 0; j <= 30; j ++) {
        long tmp = 1;
        
        for (int k = 0; k < j; k ++) {
          tmp *= (d - i - k);
          tmp %= mod;
          tmp *= invl(k + 1, mod);
          tmp %= mod;
        }
        c[i][j] = tmp;
      }
    }
    
    // dp[k][j] k個満たしてないものがある その合計はjである組み合わせ
    long[][] dp = new long[n + 1][max * 2];
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int k = n - 1; k >= 0; k--) {
        for (int l = max; l >= 0; l--) {
          for (int j = 0; j < a[i]; j++) {
            dp[k + 1][j + l] += dp[k][l] * c[l][j] % mod;
            dp[k + 1][j + l] %= mod;
          }
        }
      }
    }

    long ret = 0;
    for (int k = 0; k <= n; k++) {
      for (int j = 0; j <= max; j++) {
        long now = powMod(n - k, d - j, mod) * dp[k][j] % mod;

        if (k % 2 == 0) {
          ret += now;
        } else {
          ret += mod - now;
        }
        ret %= mod;
      }
    }
    System.out.println(ret);
  }

  public static long powMod(long x, long k, long mod) {
    int n = 63 - Long.numberOfLeadingZeros(k);
    long answer = 1;
    for (int i = n; i >= 0; i--) {
      answer = (answer * answer) % mod;
      if (((k >> i) & 1L) == 1L) {
        answer = (answer * x) % mod;
      }
    }
    return answer;
  }

  public static long invl(long a, long mod) {
    long b = mod;
    long p = 1, q = 0;
    while (b > 0) {
        long c = a / b;
        long d;
        d = a;
        a = b;
        b = d % b;
        d = p;
        p = q;
        q = d - c * q;
    }
    return p < 0 ? p + mod : p;
}

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = System.getProperty("debug");
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}

Submission Info

Submission Time
Task D - 天下一ボディービルコンテスト
User hiromi_ayase
Language Java8 (OpenJDK 1.8.0)
Score 280
Code Size 4420 Byte
Status AC
Exec Time 667 ms
Memory 24868 KB

Judge Result

Set Name level1 level2 level3
Score / Max Score 40 / 40 100 / 100 140 / 140
Status
AC × 27
AC × 50
AC × 74
Set Name Test Cases
level1 level1_max_0.txt, level1_maxrand_0.txt, level1_maxrand_1.txt, level1_maxrand_2.txt, level1_maxrand_3.txt, level1_maxrand_4.txt, level1_maxrand_5.txt, level1_maxrand_6.txt, level1_maxrand_7.txt, level1_maxrand_8.txt, level1_maxrand_9.txt, level1_min1_0.txt, level1_min2_0.txt, level1_min3_0.txt, level1_min4_0.txt, level1_rand_0.txt, level1_rand_1.txt, level1_rand_2.txt, level1_rand_3.txt, level1_rand_4.txt, level1_rand_5.txt, level1_rand_6.txt, level1_rand_7.txt, level1_rand_8.txt, level1_rand_9.txt, level1_sample1.txt, level1_sample2.txt
level2 level1_max_0.txt, level1_maxrand_0.txt, level1_maxrand_1.txt, level1_maxrand_2.txt, level1_maxrand_3.txt, level1_maxrand_4.txt, level1_maxrand_5.txt, level1_maxrand_6.txt, level1_maxrand_7.txt, level1_maxrand_8.txt, level1_maxrand_9.txt, level1_min1_0.txt, level1_min2_0.txt, level1_min3_0.txt, level1_min4_0.txt, level1_rand_0.txt, level1_rand_1.txt, level1_rand_2.txt, level1_rand_3.txt, level1_rand_4.txt, level1_rand_5.txt, level1_rand_6.txt, level1_rand_7.txt, level1_rand_8.txt, level1_rand_9.txt, level1_sample1.txt, level1_sample2.txt, level2_max_0.txt, level2_maxrand_0.txt, level2_maxrand_1.txt, level2_maxrand_2.txt, level2_maxrand_3.txt, level2_maxrand_4.txt, level2_maxrand_5.txt, level2_maxrand_6.txt, level2_maxrand_7.txt, level2_maxrand_8.txt, level2_maxrand_9.txt, level2_min1_0.txt, level2_min2_0.txt, level2_rand_0.txt, level2_rand_1.txt, level2_rand_2.txt, level2_rand_3.txt, level2_rand_4.txt, level2_rand_5.txt, level2_rand_6.txt, level2_rand_7.txt, level2_rand_8.txt, level2_rand_9.txt
level3 level1_max_0.txt, level1_maxrand_0.txt, level1_maxrand_1.txt, level1_maxrand_2.txt, level1_maxrand_3.txt, level1_maxrand_4.txt, level1_maxrand_5.txt, level1_maxrand_6.txt, level1_maxrand_7.txt, level1_maxrand_8.txt, level1_maxrand_9.txt, level1_min1_0.txt, level1_min2_0.txt, level1_min3_0.txt, level1_min4_0.txt, level1_rand_0.txt, level1_rand_1.txt, level1_rand_2.txt, level1_rand_3.txt, level1_rand_4.txt, level1_rand_5.txt, level1_rand_6.txt, level1_rand_7.txt, level1_rand_8.txt, level1_rand_9.txt, level1_sample1.txt, level1_sample2.txt, level2_max_0.txt, level2_maxrand_0.txt, level2_maxrand_1.txt, level2_maxrand_2.txt, level2_maxrand_3.txt, level2_maxrand_4.txt, level2_maxrand_5.txt, level2_maxrand_6.txt, level2_maxrand_7.txt, level2_maxrand_8.txt, level2_maxrand_9.txt, level2_min1_0.txt, level2_min2_0.txt, level2_rand_0.txt, level2_rand_1.txt, level2_rand_2.txt, level2_rand_3.txt, level2_rand_4.txt, level2_rand_5.txt, level2_rand_6.txt, level2_rand_7.txt, level2_rand_8.txt, level2_rand_9.txt, level3_max_0.txt, level3_maxrand_0.txt, level3_maxrand_1.txt, level3_maxrand_2.txt, level3_maxrand_3.txt, level3_maxrand_4.txt, level3_maxrand_5.txt, level3_maxrand_6.txt, level3_maxrand_7.txt, level3_maxrand_8.txt, level3_maxrand_9.txt, level3_min1_0.txt, level3_min2_0.txt, level3_rand_0.txt, level3_rand_1.txt, level3_rand_2.txt, level3_rand_3.txt, level3_rand_4.txt, level3_rand_5.txt, level3_rand_6.txt, level3_rand_7.txt, level3_rand_8.txt, level3_rand_9.txt, level3_sample3.txt
Case Name Status Exec Time Memory
level1_max_0.txt AC 136 ms 22540 KB
level1_maxrand_0.txt AC 137 ms 19892 KB
level1_maxrand_1.txt AC 137 ms 22412 KB
level1_maxrand_2.txt AC 137 ms 20524 KB
level1_maxrand_3.txt AC 136 ms 20556 KB
level1_maxrand_4.txt AC 133 ms 18828 KB
level1_maxrand_5.txt AC 135 ms 20656 KB
level1_maxrand_6.txt AC 136 ms 22576 KB
level1_maxrand_7.txt AC 138 ms 22448 KB
level1_maxrand_8.txt AC 137 ms 19248 KB
level1_maxrand_9.txt AC 134 ms 19540 KB
level1_min1_0.txt AC 115 ms 19412 KB
level1_min2_0.txt AC 128 ms 19924 KB
level1_min3_0.txt AC 145 ms 19852 KB
level1_min4_0.txt AC 119 ms 20436 KB
level1_rand_0.txt AC 119 ms 23252 KB
level1_rand_1.txt AC 125 ms 18772 KB
level1_rand_2.txt AC 127 ms 21712 KB
level1_rand_3.txt AC 119 ms 21844 KB
level1_rand_4.txt AC 140 ms 21968 KB
level1_rand_5.txt AC 137 ms 21132 KB
level1_rand_6.txt AC 141 ms 20364 KB
level1_rand_7.txt AC 128 ms 19924 KB
level1_rand_8.txt AC 120 ms 21716 KB
level1_rand_9.txt AC 127 ms 21588 KB
level1_sample1.txt AC 120 ms 19668 KB
level1_sample2.txt AC 125 ms 21204 KB
level2_max_0.txt AC 133 ms 20528 KB
level2_maxrand_0.txt AC 137 ms 18992 KB
level2_maxrand_1.txt AC 135 ms 21808 KB
level2_maxrand_2.txt AC 134 ms 20308 KB
level2_maxrand_3.txt AC 136 ms 22568 KB
level2_maxrand_4.txt AC 137 ms 20908 KB
level2_maxrand_5.txt AC 136 ms 20620 KB
level2_maxrand_6.txt AC 136 ms 22668 KB
level2_maxrand_7.txt AC 136 ms 20688 KB
level2_maxrand_8.txt AC 136 ms 22096 KB
level2_maxrand_9.txt AC 137 ms 22740 KB
level2_min1_0.txt AC 129 ms 21076 KB
level2_min2_0.txt AC 125 ms 20052 KB
level2_rand_0.txt AC 116 ms 21972 KB
level2_rand_1.txt AC 116 ms 19924 KB
level2_rand_2.txt AC 128 ms 19960 KB
level2_rand_3.txt AC 118 ms 21588 KB
level2_rand_4.txt AC 133 ms 20300 KB
level2_rand_5.txt AC 134 ms 23088 KB
level2_rand_6.txt AC 135 ms 22156 KB
level2_rand_7.txt AC 115 ms 19088 KB
level2_rand_8.txt AC 117 ms 19540 KB
level2_rand_9.txt AC 119 ms 21716 KB
level3_max_0.txt AC 667 ms 23200 KB
level3_maxrand_0.txt AC 430 ms 22944 KB
level3_maxrand_1.txt AC 398 ms 22304 KB
level3_maxrand_2.txt AC 421 ms 20900 KB
level3_maxrand_3.txt AC 413 ms 21152 KB
level3_maxrand_4.txt AC 397 ms 21536 KB
level3_maxrand_5.txt AC 441 ms 19616 KB
level3_maxrand_6.txt AC 439 ms 21272 KB
level3_maxrand_7.txt AC 401 ms 22816 KB
level3_maxrand_8.txt AC 423 ms 22816 KB
level3_maxrand_9.txt AC 412 ms 24868 KB
level3_min1_0.txt AC 177 ms 19664 KB
level3_min2_0.txt AC 116 ms 21844 KB
level3_rand_0.txt AC 309 ms 20768 KB
level3_rand_1.txt AC 273 ms 17440 KB
level3_rand_2.txt AC 133 ms 20292 KB
level3_rand_3.txt AC 198 ms 21524 KB
level3_rand_4.txt AC 135 ms 18864 KB
level3_rand_5.txt AC 198 ms 17440 KB
level3_rand_6.txt AC 189 ms 20896 KB
level3_rand_7.txt AC 265 ms 19492 KB
level3_rand_8.txt AC 173 ms 20768 KB
level3_rand_9.txt AC 157 ms 17140 KB
level3_sample3.txt AC 164 ms 20768 KB