結果
| 問題 | No.389 ロジックパズルの組み合わせ | 
| コンテスト | |
| ユーザー |  btk | 
| 提出日時 | 2016-07-08 22:40:51 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 39 ms / 2,000 ms | 
| コード長 | 1,099 bytes | 
| コンパイル時間 | 1,523 ms | 
| コンパイル使用メモリ | 168,984 KB | 
| 実行使用メモリ | 15,232 KB | 
| 最終ジャッジ日時 | 2024-10-13 06:07:03 | 
| 合計ジャッジ時間 | 5,488 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 99 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
struct INIT{INIT(){ios::sync_with_stdio(false);cin.tie(0);cout<<fixed;cout<<setprecision(10);}}init;
#define rep(i,n) for(auto i=(n)*0;i<n;i++)
#define range(it,v) for(auto &it:v)
typedef long long LL;
const LL MOD=1e9+7;
LL fact[2000000];
struct init2{
    init2(){
	fact[0]=1;
	rep(i,1500000)fact[i+1]=fact[i]*(i+1)%MOD;
    }
}i2;
// a x + b y = gcd(a, b)
// O(log (a+b) )
LL extgcd(LL a, LL b, LL &x, LL &y) {
  LL g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
// mを法とするaの逆元
// O(log a)
LL invMod(LL a) {
	LL x, y;
	if (extgcd(a, MOD, x, y) == 1)return (x + MOD) % MOD;
	else	 return 0; // unsolvable
}
LL Comb(LL n,LL k){
	LL u=fact[n];
	LL d=(fact[k]*fact[n-k])%MOD;
	return (u*invMod(d))%MOD;
}
int main(){
    LL M,rest;
    cin>>M;
    rest=M;
    int N=0;
    for(int num;cin>>num;N++){
	rest-=num;
    }
    if(rest==M)cout<<1<<endl;
    else{
	if(rest<N-1){
	    cout<<"NA"<<endl;
	}
	else{
	    rest-=N-1;
	    cout<<Comb(N+rest,N)<<endl;
	}
    }
    
    return 0;
}
            
            
            
        