結果

問題 No.616 へんなソート
ユーザー karinohitokarinohito
提出日時 2022-03-25 18:03:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 6,502 bytes
コンパイル時間 3,831 ms
コンパイル使用メモリ 234,796 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-14 02:33:41
合計ジャッジ時間 5,376 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 4 ms
5,248 KB
testcase_13 AC 57 ms
5,248 KB
testcase_14 AC 3 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 48 ms
5,248 KB
testcase_18 AC 22 ms
5,248 KB
testcase_19 AC 17 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 8 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 7 ms
5,248 KB
testcase_27 AC 7 ms
5,248 KB
testcase_28 AC 104 ms
5,248 KB
testcase_29 AC 104 ms
5,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:14: warning: "rep" redefined
   14 | #define rep(i, n) rep2(i, 0, n)
      | 
main.cpp:7: note: this is the location of the previous definition
    7 | #define rep(i, n) for (int i = 0; i < (int)(n); i++)
      | 
main.cpp: In member function 'FormalPowerSeries<T>::F& FormalPowerSeries<T>::operator*=(std::vector<std::pair<int, E> >)':
main.cpp:131:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  131 |         auto [d, c] = g.front();
      |              ^
main.cpp:136:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  136 |             for (auto& [j, b] : g) {
      |                        ^
main.cpp: In member function 'FormalPowerSeries<T>::F& FormalPowerSeries<T>::operator/=(std::vector<std::pair<int, E> >)':
main.cpp:145:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  145 |         auto [d, c] = g.front();
      |              ^
main.cpp:150:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  150 |             for (auto& [j, b] : g) {
      |                        ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
using vvll = vector<vll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
vvll G;


#include<atcoder/all>
using namespace atcoder;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)

template<class T>
struct FormalPowerSeries : vector<T> {
    using vector<T>::vector;
    using vector<T>::operator=;
    using F = FormalPowerSeries;

    F operator-() const {
        F res(*this);
        for (auto& e : res) e = -e;
        return res;
    }
    F& operator*=(const T& g) {
        for (auto& e : *this) e *= g;
        return *this;
    }
    F& operator/=(const T& g) {
        assert(g != T(0));
        *this *= g.inv();
        return *this;
    }
    F& operator+=(const F& g) {
        int n = (*this).size(), m = g.size();
        rep(i, min(n, m)) (*this)[i] += g[i];
        return *this;
    }
    F& operator-=(const F& g) {
        int n = (*this).size(), m = g.size();
        rep(i, min(n, m)) (*this)[i] -= g[i];
        return *this;
    }
    F& operator<<=(const int d) {
        int n = (*this).size();
        (*this).insert((*this).begin(), d, 0);
        (*this).resize(n);
        return *this;
    }
    F& operator>>=(const int d) {
        int n = (*this).size();
        (*this).erase((*this).begin(), (*this).begin() + min(n, d));
        (*this).resize(n);
        return *this;
    }
    F inv(int d = -1) const {
        int n = (*this).size();
        assert(n != 0 && (*this)[0] != 0);
        if (d == -1) d = n;
        assert(d > 0);
        F res{ (*this)[0].inv() };
        while (res.size() < d) {
            int m = size(res);
            F f(begin(*this), begin(*this) + min(n, 2 * m));
            F r(res);
            f.resize(2 * m), internal::butterfly(f);
            r.resize(2 * m), internal::butterfly(r);
            rep(i, 2 * m) f[i] *= r[i];
            internal::butterfly_inv(f);
            f.erase(f.begin(), f.begin() + m);
            f.resize(2 * m), internal::butterfly(f);
            rep(i, 2 * m) f[i] *= r[i];
            internal::butterfly_inv(f);
            T iz = T(2 * m).inv(); iz *= -iz;
            rep(i, m) f[i] *= iz;
            res.insert(res.end(), f.begin(), f.begin() + m);
        }
        return { res.begin(), res.begin() + d };
    }

    
     // fast: FMT-friendly modulus only
     F &operator*=(const F &g) {
       int n = (*this).size();
       *this = convolution(*this, g);
       (*this).resize(n);
       return *this;
     }

     /*
     F &operator/=(const F &g) {
       int n = (*this).size();
       *this = convolution(*this, g.inv(n));
       (*this).resize(n);
       return *this;
     }
     */
     

    /*
    // naive
     F &operator*=(const F &g) {
       int n = (*this).size(), m = g.size();
       drep(i, n) {
         (*this)[i] *= g[0];
         rep2(j, 1, min(i+1, m)) (*this)[i] += (*this)[i-j] * g[j];
       }
       return *this;
     }
     */
     F &operator/=(const F &g) {
       assert(g[0] != T(0));
       T ig0 = g[0].inv();
       int n = (*this).size(), m = g.size();
       rep(i, n) {
         rep2(j, 1, min(i+1, m)) (*this)[i] -= (*this)[i-j] * g[j];
         (*this)[i] *= ig0;
       }
       return *this;
     }
     
     

    // sparse
    F& operator*=(vector<pair<int, T>> g) {
        int n = (*this).size();
        auto [d, c] = g.front();
        if (d == 0) g.erase(g.begin());
        else c = 0;
        drep(i, n) {
            (*this)[i] *= c;
            for (auto& [j, b] : g) {
                if (j > i) break;
                (*this)[i] += (*this)[i - j] * b;
            }
        }
        return *this;
    }
    F& operator/=(vector<pair<int, T>> g) {
        int n = (*this).size();
        auto [d, c] = g.front();
        assert(d == 0 && c != T(0));
        T ic = c.inv();
        g.erase(g.begin());
        rep(i, n) {
            for (auto& [j, b] : g) {
                if (j > i) break;
                (*this)[i] -= (*this)[i - j] * b;
            }
            (*this)[i] *= ic;
        }
        return *this;
    }

    // multiply and divide (1 + cz^d)
    void multiply(const int d, const T c) {
        int n = (*this).size();
        if (c == T(1)) drep(i, n - d) (*this)[i + d] += (*this)[i];
        else if (c == T(-1)) drep(i, n - d) (*this)[i + d] -= (*this)[i];
        else drep(i, n - d) (*this)[i + d] += (*this)[i] * c;
    }
    void divide(const int d, const T c) {
        int n = (*this).size();
        if (c == T(1)) rep(i, n - d) (*this)[i + d] -= (*this)[i];
        else if (c == T(-1)) rep(i, n - d) (*this)[i + d] += (*this)[i];
        else rep(i, n - d) (*this)[i + d] -= (*this)[i] * c;
    }

    T eval(const T& a) const {
        T x(1), res(0);
        for (auto e : *this) res += e * x, x *= a;
        return res;
    }

    F operator*(const T& g) const { return F(*this) *= g; }
    F operator/(const T& g) const { return F(*this) /= g; }
    F operator+(const F& g) const { return F(*this) += g; }
    F operator-(const F& g) const { return F(*this) -= g; }
    F operator<<(const int d) const { return F(*this) <<= d; }
    F operator>>(const int d) const { return F(*this) >>= d; }
    F operator*(const F& g) const { return F(*this) *= g; }
    F operator/(const F& g) const { return F(*this) /= g; }
    F operator*(vector<pair<int, T>> g) const { return F(*this) *= g; }
    F operator/(vector<pair<int, T>> g) const { return F(*this) /= g; }
};

using mint = modint1000000007;
using F = FormalPowerSeries<mint>;


vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}




int main() {
    ll N, K;
    cin >> N >> K;
    F f = { 1 };
    f.resize(N * N);
    rep(i, N) {
        f.multiply(i + 1, -1);
        f.divide(1, -1);
    }
    f.divide(1, -1);
    cout << f[K].val() << endl;

}
0