結果

問題 No.2747 Permutation Adjacent Sum
ユーザー torisasami4torisasami4
提出日時 2024-04-21 00:53:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 345 ms / 3,000 ms
コード長 11,215 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 205,344 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2024-04-21 00:53:37
合計ジャッジ時間 10,062 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
12,160 KB
testcase_01 AC 56 ms
5,376 KB
testcase_02 AC 91 ms
8,832 KB
testcase_03 AC 36 ms
5,376 KB
testcase_04 AC 144 ms
12,672 KB
testcase_05 AC 276 ms
26,368 KB
testcase_06 AC 163 ms
13,568 KB
testcase_07 AC 138 ms
11,520 KB
testcase_08 AC 166 ms
17,536 KB
testcase_09 AC 303 ms
23,424 KB
testcase_10 AC 59 ms
5,504 KB
testcase_11 AC 118 ms
11,776 KB
testcase_12 AC 32 ms
5,376 KB
testcase_13 AC 61 ms
6,944 KB
testcase_14 AC 215 ms
16,092 KB
testcase_15 AC 242 ms
22,912 KB
testcase_16 AC 134 ms
12,160 KB
testcase_17 AC 226 ms
21,248 KB
testcase_18 AC 211 ms
20,580 KB
testcase_19 AC 20 ms
5,376 KB
testcase_20 AC 153 ms
12,800 KB
testcase_21 AC 209 ms
17,660 KB
testcase_22 AC 219 ms
18,684 KB
testcase_23 AC 94 ms
10,496 KB
testcase_24 AC 135 ms
10,880 KB
testcase_25 AC 100 ms
8,448 KB
testcase_26 AC 176 ms
16,448 KB
testcase_27 AC 185 ms
17,968 KB
testcase_28 AC 161 ms
17,280 KB
testcase_29 AC 112 ms
12,800 KB
testcase_30 AC 345 ms
26,552 KB
testcase_31 AC 320 ms
26,624 KB
testcase_32 AC 316 ms
26,532 KB
testcase_33 AC 321 ms
26,560 KB
testcase_34 AC 345 ms
26,496 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("O2,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define per(i, n) for (int i = (n)-1; 0 <= i; i--)
#define rep2(i, l, r) for (int i = (l); i < int(r); i++)
#define per2(i, l, r) for (int i = (r)-1; int(l) <= i; i--)
#define each(e, v) for (auto &e : v)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()
template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}
template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using maxheap = std::priority_queue<T>;
template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}
template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}
template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

// __int128_t gcd(__int128_t a, __int128_t b) {
//     if (a == 0)
//         return b;
//     if (b == 0)
//         return a;
//     __int128_t cnt = a % b;
//     while (cnt != 0) {
//         a = b;
//         b = cnt;
//         cnt = a % b;
//     }
//     return b;
// }

struct Union_Find_Tree {
    vector<int> data;
    const int n;
    int cnt;

    Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}

    int root(int x) {
        if (data[x] < 0) return x;
        return data[x] = root(data[x]);
    }

    int operator[](int i) { return root(i); }

    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false;
        // if (data[x] > data[y]) swap(x, y);
        data[x] += data[y], data[y] = x;
        cnt--;
        return true;
    }

    int size(int x) { return -data[root(x)]; }

    int count() { return cnt; };

    bool same(int x, int y) { return root(x) == root(y); }

    void clear() {
        cnt = n;
        fill(begin(data), end(data), -1);
    }
};

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    static int get_mod() { return mod; }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() { return *this += Mod_Int(1); }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() { return *this -= Mod_Int(1); }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const { return Mod_Int(-x); }

    Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }

    Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }

    Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }

    Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }

    bool operator==(const Mod_Int &p) const { return x == p.x; }

    bool operator!=(const Mod_Int &p) const { return x != p.x; }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

ll mpow(ll x, ll n, ll mod) {
    ll ans = 1;
    x %= mod;
    while (n != 0) {
        if (n & 1) ans = ans * x % mod;
        x = x * x % mod;
        n = n >> 1;
    }
    ans %= mod;
    return ans;
}

template <typename T>
T modinv(T a, const T &m) {
    T b = m, u = 1, v = 0;
    while (b > 0) {
        T t = a / b;
        swap(a -= t * b, b);
        swap(u -= t * v, v);
    }
    return u >= 0 ? u % m : (m - (-u) % m) % m;
}

ll divide_int(ll a, ll b) {
    if (b < 0) a = -a, b = -b;
    return (a >= 0 ? a / b : (a - b + 1) / b);
}

// const int MOD = 1000000007;
const int MOD = 998244353;
using mint = Mod_Int<MOD>;

// ----- library -------

// ラグランジュ補間 (1 点)
// 計算量 O(n)

// 概要
// n-1 次以下の多項式 f(x) について f(0),f(1),...,f(n-1) の値が与えられたときに、与えられた 1 点 c について f(c) を求める。
// ラグランジュ補間をすると、
// f(c) = Σ[0<=i<n] f(i)Π[j!=i](c-j)/(i-j)
// であるから、階乗の逆数と x-i の累積積を左右から求めることで高速化する。

// verified with
// https://atcoder.jp/contests/arc033/tasks/arc033_4

using namespace std;


// 組み合わせ
// 計算量 前計算:O(n)、二項係数:O(1)、逆数:O(1)、第 2 種スターリング数:O(k log(n))、ベル数:O(min(n,k)log(n))

// 第 2 種スターリング数:n 個の区別できる玉を、k 個の区別しない箱に、各箱に 1 個以上玉が入るように入れる場合の数
// ベル数:n 個の区別できる玉を、k 個の区別しない箱に入れる場合の数

// 概要
// 前計算:i = 0,1,...,n について i! とその逆元を求める。
// 二項係数:nCk = n!/((n-k)!*k!), nPk = n!/(n-k)!, nHk = (n+k-1)Ck
// 逆数:1/k = (k-1)!/k!
// 第 2 種スターリング数:包除原理
// ベル数:第 2 種スターリング数の和

// verified with
// https://judge.yosupo.jp/problem/binomial_coefficient_prime_mod
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_5_B&lang=ja
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_5_D&lang=ja
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_5_E&lang=ja
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_5_G&lang=ja
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_5_I&lang=ja

using namespace std;

template <typename T>
struct Combination {
    static vector<T> _fac, _ifac;

    Combination() {}

    static void init(int n) {
        _fac.resize(n + 1), _ifac.resize(n + 1);
        _fac[0] = 1;
        for (int i = 1; i <= n; i++) _fac[i] = _fac[i - 1] * i;
        _ifac[n] = _fac[n].inverse();
        for (int i = n; i >= 1; i--) _ifac[i - 1] = _ifac[i] * i;
    }

    static T fac(int k) { return _fac[k]; }

    static T ifac(int k) { return _ifac[k]; }

    static T inv(int k) { return fac(k - 1) * ifac(k); }

    static T P(int n, int k) {
        if (k < 0 || n < k) return 0;
        return fac(n) * ifac(n - k);
    }

    static T C(int n, int k) {
        if (k < 0 || n < k) return 0;
        return fac(n) * ifac(n - k) * ifac(k);
    }

    // n 個の区別できる箱に、k 個の区別できない玉を入れる場合の数
    static T H(int n, int k) {
        if (n < 0 || k < 0) return 0;
        return k == 0 ? 1 : C(n + k - 1, k);
    }

    // n 個の区別できる玉を、k 個の区別しない箱に、各箱に 1 個以上玉が入るように入れる場合の数
    static T second_stirling_number(int n, int k) {
        T ret = 0;
        for (int i = 0; i <= k; i++) {
            T tmp = C(k, i) * T(i).pow(n);
            ret += ((k - i) & 1) ? -tmp : tmp;
        }
        return ret * ifac(k);
    }

    // n 個の区別できる玉を、k 個の区別しない箱に入れる場合の数
    static T bell_number(int n, int k) {
        if (n == 0) return 1;
        k = min(k, n);
        vector<T> pref(k + 1);
        pref[0] = 1;
        for (int i = 1; i <= k; i++) {
            if (i & 1) {
                pref[i] = pref[i - 1] - ifac(i);
            } else {
                pref[i] = pref[i - 1] + ifac(i);
            }
        }
        T ret = 0;
        for (int i = 1; i <= k; i++) ret += T(i).pow(n) * ifac(i) * pref[k - i];
        return ret;
    }
};

template <typename T>
vector<T> Combination<T>::_fac = vector<T>();

template <typename T>
vector<T> Combination<T>::_ifac = vector<T>();

// n 次多項式 f の f(0),...,f(n) を与えて f(c) を計算
// comb を n まで初期化する
template <typename T>
T single_point_interpolation(vector<T> ys, T c) {
    using comb_ = Combination<T>;
    int n = ys.size();
    T coef = 1;
    for (int i = 0; i < n; i++) {
        ys[i] *= coef * comb_::ifac(i);
        coef *= c - i;
    }
    coef = 1;
    T ret = 0;
    for (int i = n - 1; i >= 0; i--) {
        ret += ys[i] * coef * comb_::ifac(n - 1 - i) * ((n - 1 - i) & 1 ? -1 : 1);
        coef *= c - i;
    }
    return ret;
}
const vector<mint> fac{1,295201906,160030060,957629942,545208507,213689172,760025067,939830261,506268060,39806322,808258749,440133909,686156489,741797144,390377694,12629586,544711799,104121967,495867250,421290700,117153405,57084755,202713771,675932866,79781699,956276337,652678397,35212756,655645460,468129309,761699708,533047427,287671032,206068022,50865043,144980423,111276893,259415897,444094191,593907889,573994984,892454686,566073550,128761001,888483202,251718753,548033568,428105027,742756734,546182474,62402409,102052166,826426395,159186619,926316039,176055335,51568171,414163604,604947226,681666415,511621808,924112080,265769800,955559118,763148293,472709375,19536133,860830935,290471030,851685235,242726978,169855231,612759169,599797734,961628039,953297493,62806842,37844313,909741023,689361523,887890124,380694152,669317759,367270918,806951470,843736533,377403437,945260111,786127243,80918046,875880304,364983542,623250998,598764068,804930040,24257676,214821357,791011898,954947696,183092975,0};
// ----- library -------

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n, k;
    cin >> n >> k;
    vector<mint> fk(k + 2, 0), fk1(k + 3, 0);
    rep2(i, 1, k + 2) fk[i] = fk[i - 1] + mint(i).pow(k);
    rep2(i, 1, k + 3) fk1[i] = fk1[i - 1] + mint(i).pow(k + 1);
    using comb = Combination<mint>;
    comb::init(k + 10);
    mint ans = single_point_interpolation<mint>(fk, n - 1) * n - single_point_interpolation<mint>(fk1, n - 1);
    ans *= 2;
    const int v = 1e7;
    ans *= fac[(n - 1) / v];
    rep2(i, (n - 1) / v * v + 1, n) ans *= i;
    cout << ans << endl;
}
0