結果

問題 No.1001 注文の多い順列
ユーザー noisy_noiminnoisy_noimin
提出日時 2020-03-05 01:21:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,875 bytes
コンパイル時間 1,335 ms
コンパイル使用メモリ 127,716 KB
実行使用メモリ 73,728 KB
最終ジャッジ日時 2024-04-22 01:59:35
合計ジャッジ時間 3,182 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 60 ms
61,312 KB
testcase_19 AC 55 ms
55,552 KB
testcase_20 AC 35 ms
36,992 KB
testcase_21 AC 31 ms
33,536 KB
testcase_22 AC 70 ms
73,728 KB
testcase_23 AC 69 ms
73,344 KB
testcase_24 AC 69 ms
73,088 KB
testcase_25 AC 69 ms
71,552 KB
testcase_26 AC 74 ms
70,528 KB
testcase_27 AC 73 ms
71,808 KB
testcase_28 AC 73 ms
72,064 KB
testcase_29 AC 68 ms
72,832 KB
testcase_30 AC 64 ms
69,760 KB
testcase_31 AC 67 ms
72,960 KB
testcase_32 AC 67 ms
73,728 KB
testcase_33 AC 74 ms
73,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n;
    cin >> n;
    vector<Pii> p(n);
    int min_cnt = 0;
    for(int i=0;i<n;++i) {
        cin >> p[i].second >> p[i].first;
        if(p[i].second == 1) {
            --p[i].first;
        } else {
            ++min_cnt;
        }
    }
    sort(p.begin(), p.end());

    vector<vector<ll>> dp(n+1, vector<ll>(n+1, 0));
    dp[0][0] = 1;
    for(int i=0;i<n;++i) {
        for(int j=0;j<=i;++j) {
            if(p[i].second == 1) {
                dp[i+1][j] += dp[i][j];
                dp[i+1][j] %= MOD;
                dp[i+1][j+1] += dp[i][j] * max(0, p[i].first-j) % MOD;
                dp[i+1][j+1] %= MOD;
            } else {
                dp[i+1][j+1] += dp[i][j] * max(0, p[i].first-j) % MOD;
                dp[i+1][j+1] %= MOD;
            }
        }
    }

    ll fact[n+1];
    fact[0] = 1;
    for(int i=1;i<=n;++i) fact[i] = (fact[i-1] * i) % MOD;

    ll ans = 0LL;
    for(int i=min_cnt;i<=n;++i) {
        if((i-min_cnt) % 2) {
            ans += MOD - dp[n][i] * fact[n-i] % MOD;
        } else {
            ans += dp[n][i] * fact[n-i] % MOD;
        }
        ans %= MOD;
    }
    
    cout << ans << endl;
}
0