結果

問題 No.243 出席番号(2)
ユーザー 0w1
提出日時 2017-03-16 15:45:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 842 bytes
コンパイル時間 1,555 ms
コンパイル使用メモリ 168,936 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-04 02:38:54
合計ジャッジ時間 3,464 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MOD = ( int ) 1e9 + 7;
const int MAXN = 5000;

int fact[ MAXN + 1 ];
int N;
int A[ MAXN ];
int cnt[ MAXN ];
int dp[ MAXN + 1 ]; // i bad pairs -> combination, not considering other

signed main(){
  {
    for( int i = fact[ 0 ] = 1; i <= MAXN; ++i ){
      fact[ i ] = 1LL * fact[ i - 1 ] * i % MOD;
    }
  }
  ios::sync_with_stdio( 0 );
  cin >> N;
  for( int i = 0; i < N; ++i ){
    cin >> A[ i ];
    ++cnt[ A[ i ] ];
  }
  dp[ 0 ] = 1;
  for( int i = 0; i < N; ++i ){
    for( int j = N - 1; j >= 0; --j ){
      ( dp[ j + 1 ] += 1LL * cnt[ i ] * dp[ j ] % MOD ) %= MOD;
    }
  }
  int ans = 0;
  for( int i = 0; i <= N; ++i ){
    ( ans += 1LL * ( i & 1 ? - 1 : 1 ) * dp[ i ] * fact[ N - i ] % MOD ) %= MOD;
  }
  if( ans < 0 ) ans += MOD;
  cout << ans << endl;
  return 0;
}
0