結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー どらら
提出日時 2016-07-08 23:54:48
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,715 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 84,064 KB
実行使用メモリ 7,524 KB
最終ジャッジ日時 2024-10-13 07:36:53
合計ジャッジ時間 4,095 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 99
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

#define MOD 1000000007LL
ll extgcd(ll a, ll b, ll &x, ll &y){
  ll d = a;
  if(b!=0){
    d = extgcd(b,a%b,y,x);
    y -= (a/b)*x;
  }else{
    x = 1; y = 0;
  }
  return d;
}

ll mod_inverse(ll a, ll m){
  ll x, y;
  extgcd(a, m, x, y);
  return (m+x%m)%m;
}

ll mod_pow(ll x, ll n, ll mod){
  if(n==0) return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if(n & 1) res = res * x % mod;
  return res;
}

ll mod_fact(ll n, ll p){
  ll res = 1;
  while(n>0){
    ll m = n%p;
    for(ll i=1; i<=m; i++) res = (res*i)%p;
    if((n/=p)%2>0) res = p-res;
  }
  return res;
}

ll mod_comb(ll n, ll k, ll p){
  if(n<0 || k<0 || n<k) return 0;
  ll a1 = mod_fact(n, p), a2 = mod_fact(k, p), a3 = mod_fact(n-k, p);
  return a1 * mod_inverse((a2*(a3%p))%p, p) % p;
}

int main(){
  ios::sync_with_stdio(false);
  int M;
  vector<ll> H;
  cin >> M;

  ll a;
  while(cin >> a){
    H.push_back(a);
  }
  if(H.size()==1 && H[0] == 0){
    cout << 1 << endl;
  }else{
    ll n = H.size();
    ll m = 0;
    rep(i,H.size()){
      if(i!=0) m+=1;
      m += H[i];
    }
    if(M-m<0){
      cout << "NA" << endl;
    }else{
      ll res = mod_comb(M-m+n, n, MOD);
      cout << res << endl;
    }
  }
  return 0;
}

0