結果

問題 No.1001 注文の多い順列
ユーザー tempura_pptempura_pp
提出日時 2020-02-18 02:14:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,001 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 73,336 KB
実行使用メモリ 73,656 KB
最終ジャッジ日時 2024-10-06 15:21:48
合計ジャッジ時間 2,429 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>

using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const ll mod=1e9+7 ;

ll dp[3030][3030];
int main(){
    int n;
    cin>>n;
    pair<int,int> p[n];
    int cnt = 0;
    rep(i,n){
        cin>>p[i].second>>p[i].first;
        if(p[i].second==1){
            --p[i].first;++cnt;
        }
    }
    sort(p,p+n);
    dp[0][0]=1;
    rep(i,n)rep(j,i+1){
        dp[i][j]%=mod;
        int used = i-j;
        if(p[i].second==0)dp[i+1][j]+=dp[i][j]*max(p[i].first-used,0)%mod;
        if(p[i].second==1){
            dp[i+1][j]+=dp[i][j]*max(p[i].first-used,0)%mod;
            dp[i+1][j+1]+=dp[i][j];
        }
    }
    ll fact = 1;
    rep(j,n+1){
        dp[n][j]=dp[n][j]*fact%mod;
        fact =fact*(j+1)%mod;
    }
    ll ans = 0;
    rep(i,cnt+1){
        if(i%2==0)ans += dp[n][cnt-i];
        else ans += mod-dp[n][cnt-i];
    }
    cout<<ans%mod<<endl;
    return 0;
}
0