結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-06-18 14:29:39
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,133 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 60,268 KB
最終ジャッジ日時 2024-04-27 02:26:45
合計ジャッジ時間 1,268 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:7:25: error: ‘powl’ was not declared in this scope
    7 | constexpr i64 mod = i64(powl(10, 9)) + 7;
      |                         ^~~~
main.cpp: In function ‘i64 nCr(i64, i64, i64)’:
main.cpp:22:3: error: ‘vector’ was not declared in this scope
   22 |   vector<i64> fact(n+1, 1);
      |   ^~~~~~
main.cpp:4:1: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
    3 | #include <tuple>
  +++ |+#include <vector>
    4 | using namespace std;
main.cpp:22:13: error: expected primary-expression before ‘>’ token
   22 |   vector<i64> fact(n+1, 1);
      |             ^
main.cpp:22:15: error: ‘fact’ was not declared in this scope
   22 |   vector<i64> fact(n+1, 1);
      |               ^~~~
main.cpp:32:10: error: ‘inv2’ was not declared in this scope; did you mean ‘inv1’?
   32 |   res *= inv2;
      |          ^~~~
      |          inv1
main.cpp: In function ‘int main()’:
main.cpp:39:3: error: ‘vector’ was not declared in this scope
   39 |   vector<i64> a;
      |   ^~~~~~
main.cpp:39:3: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
main.cpp:39:13: error: expected primary-expression before ‘>’ token
   39 |   vector<i64> a;
      |             ^
main.cpp:39:15: error: ‘a’ was not declared in this scope
   39 |   vector<i64> a;
      |               ^
main.cpp:42:13: error: ‘accumulate’ was not declared in this scope
   42 |   i64 acc = accumulate(begin(a), end(a), 0);
      |             ^~~~~~~~~~
main.cpp:38:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |   i64 m; scanf("%lld", &m);
      |          ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <tuple>
using namespace std;
using i64 = long long;

constexpr i64 mod = i64(powl(10, 9)) + 7;

tuple<i64, i64, i64> extgcd(i64 x, i64 y) {
  if(y == 0) { return make_tuple(1, 0, x); }
  i64 a, b, d; tie(a, b, d) = extgcd(y, x%y);
  return make_tuple(b, a-x/y*b, d);
}

i64 mod_inv(const i64 &a, const i64 &m) {
  i64 x, _, d; tie(x, _, d) = extgcd(a, m);
  if(d == 1) { return (x % m + m) % m; }
  throw;
}

i64 nCr(i64 n, i64 r, i64 mod) {
  vector<i64> fact(n+1, 1);
  for(i64 i=2; i<=n; ++i) {
    fact[i] = fact[i-1] * i;
    fact[i] %= mod;
  }
  i64 inv1 = mod_inv(fact[r],   mod),
      inv2 = mod_inv(fact[n-r], mod);
  i64 res = fact[n];
  res *= inv1;
  res %= mod;
  res *= inv2;
  res %= mod;
  return res;
}

int main(void) {
  i64 m; scanf("%lld", &m);
  vector<i64> a;
  for(i64 x; ~scanf("%lld", &x); a.push_back(x)) ;
  i64 n = a.size();
  i64 acc = accumulate(begin(a), end(a), 0);
  if(a[0] == 0) {
    puts("1");
    return 0;
  }
  if(acc + n - 1 > m) {
    puts("NA");
    return 0;
  }
  i64 res = nCr(m-acc+1, n, mod);
  printf("%lld\n", res);
  return 0;
}
0