結果

問題 No.1001 注文の多い順列
ユーザー jelljell
提出日時 2020-03-16 22:31:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 6,424 bytes
コンパイル時間 1,467 ms
コンパイル使用メモリ 103,796 KB
実行使用メモリ 39,520 KB
最終ジャッジ日時 2023-08-18 19:49:30
合計ジャッジ時間 3,262 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
39,244 KB
testcase_01 AC 11 ms
39,216 KB
testcase_02 AC 11 ms
39,292 KB
testcase_03 AC 11 ms
39,272 KB
testcase_04 AC 11 ms
39,276 KB
testcase_05 AC 11 ms
39,248 KB
testcase_06 AC 11 ms
39,276 KB
testcase_07 AC 11 ms
39,400 KB
testcase_08 AC 11 ms
39,356 KB
testcase_09 AC 12 ms
39,212 KB
testcase_10 AC 10 ms
39,240 KB
testcase_11 AC 11 ms
39,212 KB
testcase_12 AC 11 ms
39,220 KB
testcase_13 AC 10 ms
39,196 KB
testcase_14 AC 12 ms
39,220 KB
testcase_15 AC 11 ms
39,220 KB
testcase_16 AC 12 ms
39,224 KB
testcase_17 AC 11 ms
39,236 KB
testcase_18 AC 29 ms
39,276 KB
testcase_19 AC 29 ms
39,256 KB
testcase_20 AC 22 ms
39,256 KB
testcase_21 AC 23 ms
39,248 KB
testcase_22 AC 35 ms
39,320 KB
testcase_23 AC 35 ms
39,264 KB
testcase_24 AC 34 ms
39,520 KB
testcase_25 AC 35 ms
39,440 KB
testcase_26 AC 53 ms
39,272 KB
testcase_27 AC 52 ms
39,268 KB
testcase_28 AC 52 ms
39,332 KB
testcase_29 AC 18 ms
39,264 KB
testcase_30 AC 20 ms
39,240 KB
testcase_31 AC 21 ms
39,408 KB
testcase_32 AC 11 ms
39,408 KB
testcase_33 AC 55 ms
39,312 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:100:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
  100 | main()
      | ^~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <bitset>
#include <functional>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <tuple>
#include <numeric>
using namespace std;

template <class T> void chmax(T &x, T y) {if(x<y) x=y;}
template <class T> void chmin(T &x, T y) {if(x>y) x=y;}

#ifndef Modint_hpp
#define Modint_hpp
#include <cassert>
#include <iostream>

template <int mod>
class modint
{
    int val;
public:
    constexpr modint() noexcept : val{0} {}
    constexpr modint(long long x) noexcept : val((x %= mod) < 0 ? mod + x : x) {}
    constexpr long long value() const noexcept { return val; }
    constexpr modint operator++(int) noexcept { modint t = *this; return ++val, t; }
    constexpr modint operator--(int) noexcept { modint t = *this; return --val, t; }
    constexpr modint &operator++() noexcept { return ++val, *this; }
    constexpr modint &operator--() noexcept { return --val, *this; }
    constexpr modint operator-() const noexcept { return modint(-val); }
    constexpr modint &operator+=(const modint &other) noexcept { return (val += other.val) < mod ? 0 : val -= mod, *this; }
    constexpr modint &operator-=(const modint &other) noexcept { return (val += mod - other.val) < mod ? 0 : val -= mod, *this; }
    constexpr modint &operator*=(const modint &other) noexcept { return val = (long long)val * other.val % mod, *this; }
    constexpr modint &operator/=(const modint &other) noexcept { return *this *= inverse(other); }
    constexpr modint operator+(const modint &other) const noexcept { return modint(*this) += other; }
    constexpr modint operator-(const modint &other) const noexcept { return modint(*this) -= other; }
    constexpr modint operator*(const modint &other) const noexcept { return modint(*this) *= other; }
    constexpr modint operator/(const modint &other) const noexcept { return modint(*this) /= other; }
    constexpr bool operator==(const modint &other) const noexcept { return val == other.val; }
    constexpr bool operator!=(const modint &other) const noexcept { return val != other.val; }
    constexpr bool operator!() const noexcept { return !val; }
    friend constexpr modint operator+(long long x, modint y) noexcept { return modint(x) + y; }
    friend constexpr modint operator-(long long x, modint y) noexcept { return modint(x) - y; }
    friend constexpr modint operator*(long long x, modint y) noexcept { return modint(x) * y; }
    friend constexpr modint operator/(long long x, modint y) noexcept { return modint(x) / y; }
    static constexpr modint inverse(const modint &other) noexcept
    {
        assert(other != 0);
        int a{mod}, b{other.val}, u{}, v{1}, t{};
        while(b) t = a / b, a ^= b ^= (a -= t * b) ^= b, u ^= v ^= (u -= t * v) ^= v;
        return {u};
    }
    static constexpr modint pow(modint other, long long e) noexcept
    {
        if(e < 0) e = e % (mod - 1) + mod - 1;
        modint res{1};
        while(e) { if(e & 1) res *= other; other *= other, e >>= 1; }
        return res;
    }
    friend std::ostream &operator<<(std::ostream &os, const modint &other) noexcept { return os << other.val; }
    friend std::istream &operator>>(std::istream &is, modint &other) noexcept { long long val; other = {(is >> val, val)}; return is; }
}; // class modint

template <>
class modint<2>
{
    bool val;
public:
    constexpr modint(bool x = false) noexcept : val{x} {}
    constexpr modint(int x) noexcept : val(x & 1) {}
    constexpr modint(long long x) noexcept : val(x & 1) {}
    constexpr operator bool() const noexcept { return val; }
    constexpr bool value() const noexcept { return val; }
    constexpr modint &operator+=(const modint &other) noexcept { return val ^= other.val, *this; }
    constexpr modint &operator-=(const modint &other) noexcept { return val ^= other.val, *this; }
    constexpr modint &operator*=(const modint &other) noexcept { return val &= other.val, *this; }
    constexpr modint &operator/=(const modint &other) noexcept { assert(other.val); return *this; }
    constexpr modint operator!() const noexcept { return !val; }
    constexpr modint operator-() const noexcept { return *this; }
    constexpr modint operator+(const modint &other) const noexcept { return val != other.val; }
    constexpr modint operator-(const modint &other) const noexcept { return val != other.val; }
    constexpr modint operator*(const modint &other) const noexcept { return val && other.val; }
    constexpr modint operator/(const modint &other) const noexcept { assert(other.val); return *this; }
    constexpr bool operator==(const modint &other) const noexcept { return val == other.val; }
    constexpr bool operator!=(const modint &other) const noexcept { return val != other.val; }
    friend constexpr modint operator+(long long x, modint y) noexcept { return x & 1 ? !y : y; }
    friend constexpr modint operator-(long long x, modint y) noexcept { return x & 1 ? !y : y; }
    friend constexpr modint operator*(long long x, modint y) noexcept { return x & 1 ? y : modint<2>{0}; }
    friend constexpr modint operator/(long long x, modint y) noexcept { assert(y.val); return x & 1 ? y : modint<2>{0}; }
    friend std::ostream &operator<<(std::ostream &os, const modint &other) noexcept { return os << other.val; }
    friend std::istream &operator>>(std::istream &is, modint &other) noexcept { long long val; other.val = (is >> val, val & 1); return is; }
}; // class modint specialization

#endif // Modint_hpp


main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    using mint=modint<1000000007>;

    int n; cin>>n;
    vector<int> ub(n),lb(n);
    int L=0,B=0;
    for(int i=0; i<n; ++i)
    {
        int t,x; cin>>t>>x;
        --x;
        if(t)
        {
            L++;
            lb[x]++;
        }
        else
        {
            B++;
            ub[x]++;
        }
    }

    vector<int> lbs(n),ubs(n);
    for(int i=0;i<n;++i)
    {
        if(i) lbs[i]=lbs[i-1]+lb[i];
        else lbs[i]=lb[i];
    }
    for(int i=0;i+1<n;++i)
    {
        ubs[i+1]=ubs[i]+ub[i];
    }

    mint dp[3030][3030];
    dp[0][0]=1;

    for(int i=0;i<=L;++i)
    {
        for(int j=0;i+j<n;++j)
        {
            const int now=i+j;
            if(lbs[now]>i) dp[i+1][j]+=(lbs[now]-i)*dp[i][j];
            if(j>=ubs[now]) dp[i][j+1]+=(j+1-ubs[now])*dp[i][j];
        }
    }

    cout << dp[L][B] << "\n";
    return 0;
}
0