結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー kzyKTkzyKT
提出日時 2016-07-08 22:49:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 2,240 bytes
コンパイル時間 1,414 ms
コンパイル使用メモリ 147,844 KB
実行使用メモリ 36,656 KB
最終ジャッジ日時 2023-08-03 08:46:54
合計ジャッジ時間 8,854 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
34,592 KB
testcase_01 AC 32 ms
34,740 KB
testcase_02 AC 32 ms
34,640 KB
testcase_03 AC 32 ms
34,680 KB
testcase_04 AC 33 ms
34,684 KB
testcase_05 AC 33 ms
34,688 KB
testcase_06 AC 33 ms
34,620 KB
testcase_07 AC 33 ms
34,612 KB
testcase_08 AC 33 ms
34,680 KB
testcase_09 AC 35 ms
34,816 KB
testcase_10 AC 35 ms
34,776 KB
testcase_11 AC 36 ms
34,768 KB
testcase_12 AC 34 ms
34,732 KB
testcase_13 AC 43 ms
35,344 KB
testcase_14 AC 36 ms
34,904 KB
testcase_15 AC 34 ms
34,708 KB
testcase_16 AC 34 ms
34,756 KB
testcase_17 AC 35 ms
34,772 KB
testcase_18 AC 51 ms
36,372 KB
testcase_19 AC 33 ms
34,672 KB
testcase_20 AC 33 ms
34,688 KB
testcase_21 AC 33 ms
34,844 KB
testcase_22 AC 31 ms
34,744 KB
testcase_23 AC 33 ms
34,592 KB
testcase_24 AC 33 ms
34,600 KB
testcase_25 AC 33 ms
34,636 KB
testcase_26 AC 33 ms
34,592 KB
testcase_27 AC 33 ms
34,852 KB
testcase_28 AC 33 ms
34,604 KB
testcase_29 AC 33 ms
34,604 KB
testcase_30 AC 33 ms
34,600 KB
testcase_31 AC 32 ms
34,624 KB
testcase_32 AC 33 ms
34,600 KB
testcase_33 AC 33 ms
34,584 KB
testcase_34 AC 33 ms
34,628 KB
testcase_35 AC 33 ms
34,676 KB
testcase_36 AC 33 ms
34,616 KB
testcase_37 AC 33 ms
34,744 KB
testcase_38 AC 33 ms
34,744 KB
testcase_39 AC 33 ms
34,584 KB
testcase_40 AC 32 ms
34,604 KB
testcase_41 AC 33 ms
34,748 KB
testcase_42 AC 33 ms
34,624 KB
testcase_43 AC 33 ms
34,616 KB
testcase_44 AC 33 ms
34,796 KB
testcase_45 AC 33 ms
34,632 KB
testcase_46 AC 33 ms
34,748 KB
testcase_47 AC 33 ms
34,744 KB
testcase_48 AC 33 ms
34,688 KB
testcase_49 AC 54 ms
36,368 KB
testcase_50 AC 34 ms
34,948 KB
testcase_51 AC 33 ms
34,776 KB
testcase_52 AC 36 ms
34,940 KB
testcase_53 AC 34 ms
34,776 KB
testcase_54 AC 38 ms
34,960 KB
testcase_55 AC 33 ms
34,824 KB
testcase_56 AC 34 ms
34,736 KB
testcase_57 AC 41 ms
35,424 KB
testcase_58 AC 50 ms
36,504 KB
testcase_59 AC 45 ms
35,664 KB
testcase_60 AC 38 ms
35,176 KB
testcase_61 AC 60 ms
36,656 KB
testcase_62 AC 38 ms
34,996 KB
testcase_63 AC 50 ms
36,500 KB
testcase_64 AC 37 ms
34,920 KB
testcase_65 AC 35 ms
34,836 KB
testcase_66 AC 38 ms
34,968 KB
testcase_67 AC 37 ms
34,920 KB
testcase_68 AC 38 ms
34,916 KB
testcase_69 AC 33 ms
34,604 KB
testcase_70 AC 33 ms
34,596 KB
testcase_71 AC 33 ms
34,612 KB
testcase_72 AC 33 ms
34,584 KB
testcase_73 AC 32 ms
34,604 KB
testcase_74 AC 33 ms
34,684 KB
testcase_75 AC 33 ms
34,600 KB
testcase_76 AC 32 ms
34,588 KB
testcase_77 AC 33 ms
34,624 KB
testcase_78 AC 33 ms
34,600 KB
testcase_79 AC 32 ms
34,588 KB
testcase_80 AC 33 ms
34,600 KB
testcase_81 AC 33 ms
34,740 KB
testcase_82 AC 33 ms
34,596 KB
testcase_83 AC 32 ms
34,640 KB
testcase_84 AC 32 ms
34,624 KB
testcase_85 AC 33 ms
34,596 KB
testcase_86 AC 33 ms
34,588 KB
testcase_87 AC 33 ms
34,580 KB
testcase_88 AC 33 ms
34,592 KB
testcase_89 AC 40 ms
35,024 KB
testcase_90 AC 44 ms
35,420 KB
testcase_91 AC 52 ms
36,364 KB
testcase_92 AC 35 ms
34,744 KB
testcase_93 AC 45 ms
35,728 KB
testcase_94 AC 33 ms
34,668 KB
testcase_95 AC 46 ms
35,572 KB
testcase_96 AC 43 ms
35,396 KB
testcase_97 AC 42 ms
35,312 KB
testcase_98 AC 37 ms
35,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
template<C T>void pr(T a){cout<<a;ln;}
template<C T,C T2>void pr(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
void extended_euclid(ll x,ll y,ll *c,ll *a,ll *b){
  ll a0,a1,a2,b0,b1,b2,r0,r1,r2,q;
  r0=x;r1=y;a0=1;a1=0;b0=0;b1=1;
  while(r1>0){
    q=r0/r1;r2=r0%r1;a2=a0-q*a1;b2=b0-q*b1;
    r0=r1;r1=r2;a0=a1;a1=a2;b0=b1;b1=b2;
  }
  *c=r0;*a=a0;*b=b0;
}
ll get_inv(ll n, ll p){
  ll a,b,c;
  extended_euclid(n,p,&c,&a,&b);
  if(a<p) a+=p;
  return a%p;
}
ll fact[2000100],fact_inv[2000100];
ll nCr(int N, int K){
  if(K<0||K>N) return 0;
  return fact[N]*fact_inv[K]%MAX*fact_inv[N-K]%MAX;
}
ll nHr(ll N, ll K){
  if(N==0 && K==0) return 1;
  if(N==0) return 0;
  return nCr(N+K-1, K);
}
void init() {
  fact[0]=1;
  REP(i,1,2000100) fact[i]=fact[i-1]*i%MAX;
  fact_inv[2000050]=get_inv(fact[2000050],MAX);
  for(int i=2000050-1;i>=0;i--) fact_inv[i]=fact_inv[i+1]*(i+1)%MAX;
}
void Main() {
  init();
  ll n,x,m=0,sum=0;
  cin >> n;
  string s;
  stringstream ss;
  getline(cin,s);
  getline(cin,s);
  ss << s;
  while(ss >> x) {
    m++;
    sum+=x;
  }
  if(sum+m-1>n) {
    pr("NA");
    return;
  } else if(sum==0) {
    pr(1);
    return;
  } else if(sum==n) {
    pr(1);
    return;
  }
  n-=sum;
  ll ans=nCr(n-1,m-2);
  if(m<=n) ans+=nCr(n-1,m-1)*2;
  if(m+1<=n) ans+=nCr(n-1,m);
  pr(ans%MAX);
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}
0