結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー alpha_virginisalpha_virginis
提出日時 2016-10-27 08:09:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,429 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 32,456 KB
実行使用メモリ 11,844 KB
最終ジャッジ日時 2023-08-15 19:34:11
合計ジャッジ時間 4,719 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
11,760 KB
testcase_01 AC 10 ms
11,728 KB
testcase_02 AC 10 ms
11,772 KB
testcase_03 AC 11 ms
11,724 KB
testcase_04 AC 11 ms
11,752 KB
testcase_05 AC 11 ms
11,728 KB
testcase_06 AC 10 ms
11,700 KB
testcase_07 AC 10 ms
11,728 KB
testcase_08 AC 11 ms
11,680 KB
testcase_09 AC 13 ms
11,780 KB
testcase_10 AC 12 ms
11,796 KB
testcase_11 AC 14 ms
11,764 KB
testcase_12 AC 12 ms
11,744 KB
testcase_13 AC 23 ms
11,764 KB
testcase_14 AC 15 ms
11,740 KB
testcase_15 AC 12 ms
11,744 KB
testcase_16 AC 13 ms
11,768 KB
testcase_17 AC 14 ms
11,736 KB
testcase_18 AC 34 ms
11,692 KB
testcase_19 AC 10 ms
11,772 KB
testcase_20 AC 11 ms
11,788 KB
testcase_21 AC 11 ms
11,692 KB
testcase_22 AC 11 ms
11,736 KB
testcase_23 AC 11 ms
11,724 KB
testcase_24 AC 11 ms
11,700 KB
testcase_25 AC 11 ms
11,784 KB
testcase_26 AC 10 ms
11,744 KB
testcase_27 AC 10 ms
11,724 KB
testcase_28 AC 10 ms
11,724 KB
testcase_29 AC 10 ms
11,720 KB
testcase_30 AC 11 ms
11,732 KB
testcase_31 AC 10 ms
11,724 KB
testcase_32 AC 11 ms
11,716 KB
testcase_33 AC 11 ms
11,736 KB
testcase_34 AC 11 ms
11,724 KB
testcase_35 AC 11 ms
11,760 KB
testcase_36 AC 11 ms
11,724 KB
testcase_37 AC 10 ms
11,820 KB
testcase_38 AC 10 ms
11,784 KB
testcase_39 AC 10 ms
11,764 KB
testcase_40 AC 10 ms
11,732 KB
testcase_41 AC 10 ms
11,736 KB
testcase_42 AC 10 ms
11,828 KB
testcase_43 AC 10 ms
11,692 KB
testcase_44 AC 11 ms
11,724 KB
testcase_45 AC 11 ms
11,756 KB
testcase_46 AC 10 ms
11,792 KB
testcase_47 AC 11 ms
11,732 KB
testcase_48 AC 11 ms
11,720 KB
testcase_49 AC 36 ms
11,728 KB
testcase_50 AC 12 ms
11,756 KB
testcase_51 AC 11 ms
11,564 KB
testcase_52 AC 15 ms
11,724 KB
testcase_53 AC 13 ms
11,636 KB
testcase_54 AC 16 ms
11,636 KB
testcase_55 AC 11 ms
11,656 KB
testcase_56 AC 12 ms
11,756 KB
testcase_57 AC 22 ms
11,676 KB
testcase_58 AC 31 ms
11,700 KB
testcase_59 AC 26 ms
11,640 KB
testcase_60 AC 18 ms
11,656 KB
testcase_61 AC 44 ms
11,720 KB
testcase_62 AC 17 ms
11,680 KB
testcase_63 AC 31 ms
11,724 KB
testcase_64 AC 15 ms
11,696 KB
testcase_65 AC 14 ms
11,680 KB
testcase_66 AC 16 ms
11,752 KB
testcase_67 AC 16 ms
11,660 KB
testcase_68 AC 16 ms
11,684 KB
testcase_69 AC 10 ms
11,732 KB
testcase_70 AC 10 ms
11,756 KB
testcase_71 AC 10 ms
11,764 KB
testcase_72 AC 10 ms
11,720 KB
testcase_73 AC 11 ms
11,704 KB
testcase_74 AC 10 ms
11,696 KB
testcase_75 AC 11 ms
11,764 KB
testcase_76 AC 11 ms
11,840 KB
testcase_77 AC 10 ms
11,744 KB
testcase_78 AC 10 ms
11,784 KB
testcase_79 AC 10 ms
11,720 KB
testcase_80 AC 10 ms
11,744 KB
testcase_81 AC 10 ms
11,784 KB
testcase_82 AC 10 ms
11,760 KB
testcase_83 AC 10 ms
11,744 KB
testcase_84 AC 10 ms
11,844 KB
testcase_85 AC 10 ms
11,732 KB
testcase_86 AC 10 ms
11,764 KB
testcase_87 AC 10 ms
11,784 KB
testcase_88 AC 10 ms
11,748 KB
testcase_89 AC 18 ms
11,736 KB
testcase_90 AC 24 ms
11,832 KB
testcase_91 AC 34 ms
11,768 KB
testcase_92 AC 14 ms
11,692 KB
testcase_93 AC 26 ms
11,760 KB
testcase_94 AC 11 ms
11,700 KB
testcase_95 AC 26 ms
11,744 KB
testcase_96 AC 23 ms
11,736 KB
testcase_97 AC 22 ms
11,784 KB
testcase_98 AC 16 ms
11,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>

#include <cassert>
template<long size, long mod>
struct smallCombinationTable {
  long table[size][size];
  smallCombinationTable() {
    table[0][0] = 1;
    for(int i = 1; i < size; ++i) {
      table[i][0] = table[i][i] = 1;
      for(int j = 1; j < i; ++j) {
        table[i][j] = (table[i-1][j-1] + table[i-1][j]) % mod;
      }
    }
  }
  long operator() (long n, long r) {
    assert( 0 <= n and n < size );
    assert( 0 <= r and r < size );
    return table[n][r];
  }
};

template<long size, long mod> 
struct factorialTable {
  long table[size];
  factorialTable() {
    table[0] = 1;
    for(int i = 1; i < size; ++i) {
      table[i] = (table[i-1] * i) % mod;
    }
  }
  long operator() (long n) {
    assert( 0 <= n and n < size );
    return table[n];
  }
};

template<typename T>
T pow(T x, long n) {
  T res = 1;
  T p = x;
  while( n != 0 ) {
    if( n & 0x01 ) res *= p;
    p *= p;
    n >>= 1;
  }
  return res;
}

template<long P = 1000000007>
struct Mod {
  Mod() : a(0) {}
  Mod(long x) : a(x%P) {}
  long get() { return a; }
  Mod operator + (Mod x) { return Mod<P>((a + x.get()) % P); }
  Mod operator += (Mod x) { a = (a + x.get()) % P; return *this; }
  Mod operator - (Mod x) { return Mod<P>((a + P - x.get()) % P); }
  Mod operator -= (Mod x) { a = (a + P - x.a) % P; return *this; }
  Mod operator * (Mod x) { return Mod<P>((a * x.get()) % P); }
  Mod operator *= (Mod x) { a = (a * x.a) % P; return *this; }
  Mod operator / (Mod x) { return *this * pow(x, P - 2); }
  Mod operator /= (Mod x) { a = (*this * pow(x, P - 2)); return *this; }
  long a;
};

template<long size, long mod>
struct Combination {
  factorialTable<size, mod> factorial;
  long operator() (long n, long r) {
    Mod<mod> res = Mod<mod>(factorial(n)) / Mod<mod>(factorial(r) * factorial(n-r));
    return res.get();
  }
};
Combination<1123456, 1000000007> combination;

int main() {
  long m;
  long temp;
  long total = 0;
  long count = 0;
  scanf("%ld\n", &m);
  for(;;) {
    int c = getchar();
    if( c == '\n' ) break;
    ungetc(c, stdin);
    scanf("%ld", &temp);
    total += temp;
    count += 1;
  }
  if( count == 1 and total == 0 ) {
    printf("1\n");
    return 0;
  }
  long n = count + 1;
  long r = m - total - (count - 1);
  // printf("(n, r) = (%ld, %ld)\n", n, r);
  if( r < 0 ) {
    printf("NA\n");
    return 0;
  }
  printf("%ld\n", combination(n + r - 1, r));
  
  return 0;
}
0