結果

問題 No.2747 Permutation Adjacent Sum
ユーザー KudeKude
提出日時 2024-04-21 14:25:19
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 3,000 ms
コード長 3,714 bytes
コンパイル時間 3,845 ms
コンパイル使用メモリ 299,956 KB
実行使用メモリ 22,820 KB
最終ジャッジ日時 2024-04-21 14:25:31
合計ジャッジ時間 10,194 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
15,488 KB
testcase_01 AC 65 ms
11,616 KB
testcase_02 AC 86 ms
13,836 KB
testcase_03 AC 43 ms
11,360 KB
testcase_04 AC 120 ms
15,872 KB
testcase_05 AC 217 ms
22,544 KB
testcase_06 AC 141 ms
16,172 KB
testcase_07 AC 124 ms
15,104 KB
testcase_08 AC 135 ms
18,108 KB
testcase_09 AC 218 ms
21,008 KB
testcase_10 AC 66 ms
12,288 KB
testcase_11 AC 98 ms
15,360 KB
testcase_12 AC 40 ms
11,264 KB
testcase_13 AC 58 ms
12,800 KB
testcase_14 AC 152 ms
17,536 KB
testcase_15 AC 189 ms
20,788 KB
testcase_16 AC 112 ms
15,488 KB
testcase_17 AC 184 ms
20,024 KB
testcase_18 AC 172 ms
19,624 KB
testcase_19 AC 29 ms
11,648 KB
testcase_20 AC 129 ms
15,764 KB
testcase_21 AC 169 ms
18,304 KB
testcase_22 AC 191 ms
18,704 KB
testcase_23 AC 84 ms
14,476 KB
testcase_24 AC 117 ms
14,820 KB
testcase_25 AC 94 ms
13,480 KB
testcase_26 AC 142 ms
17,536 KB
testcase_27 AC 145 ms
18,560 KB
testcase_28 AC 128 ms
17,936 KB
testcase_29 AC 98 ms
15,912 KB
testcase_30 AC 257 ms
22,820 KB
testcase_31 AC 267 ms
22,656 KB
testcase_32 AC 251 ms
22,696 KB
testcase_33 AC 263 ms
22,792 KB
testcase_34 AC 253 ms
22,820 KB
testcase_35 AC 15 ms
11,264 KB
testcase_36 AC 16 ms
11,264 KB
testcase_37 AC 15 ms
11,392 KB
testcase_38 AC 16 ms
11,392 KB
testcase_39 AC 15 ms
11,136 KB
testcase_40 AC 17 ms
11,392 KB
testcase_41 AC 15 ms
11,136 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:49:6: warning: '{anonymous}::mint {anonymous}::perm(int, int)' defined but not used [-Wunused-function]
   49 | mint perm(int n, int k) {
      |      ^~~~
main.cpp:48:6: warning: '{anonymous}::mint {anonymous}::fact(int)' defined but not used [-Wunused-function]
   48 | mint fact(int n) {return Fact[n];}
      |      ^~~~
main.cpp:44:6: warning: '{anonymous}::mint {anonymous}::icomb(int, int)' defined but not used [-Wunused-function]
   44 | mint icomb(int n, int k) {
      |      ^~~~~
main.cpp:37:6: warning: '{anonymous}::mint {anonymous}::comb(int, int)' defined but not used [-Wunused-function]
   37 | mint comb(int n, int k) {
      |      ^~~~

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

constexpr int FACT_SIZE = 1000000;
mint Fact[FACT_SIZE + 1];
mint iFact[FACT_SIZE + 1];
const auto fact_init = [] {
    Fact[0] = mint::raw(1);
    for(int i = 1; i <= FACT_SIZE; ++i) {
        Fact[i] = Fact[i-1] * i;
    }
    iFact[FACT_SIZE] = Fact[FACT_SIZE].inv();
    for(int i = FACT_SIZE; i; --i) {
        iFact[i-1] = iFact[i] * i;
    }
    return false;
}();

mint comb(int n, int k) {
    if (k == 0) return mint::raw(1);
    assert(n >= 0 && k >= 0);
    if (k > n) return mint::raw(0);
    return Fact[n] * iFact[n - k] * iFact[k];
}

mint icomb(int n, int k) {
    return iFact[n] * Fact[n - k] * Fact[k];
}

mint fact(int n) {return Fact[n];}
mint perm(int n, int k) {
    assert(0 <= n);
    return Fact[n] * iFact[n - k];
}

mint interpolate(std::vector<mint>& dataset, int x) {
    int n = dataset.size();
    std::vector<mint> prods_f(n + 1), prods_b(n + 1);
    prods_f[0] = prods_b[n] = mint::raw(1);
    for(int i = 0; i < n; ++i) {
        prods_f[i + 1] = prods_f[i] * (x - i);
    }
    for(int i = n - 1; i >= 0; --i) {
        prods_b[i] = prods_b[i + 1] * (x - i);
    }
    mint ret = mint::raw(0);
    for(int i = 0; i < n; ++i) {
        int rcnt = n - 1 - i;
        if (rcnt & 1) {
            ret -= prods_f[i] * prods_b[i + 1] * iFact[i] * iFact[rcnt] * dataset[i];
        } else {
            ret += prods_f[i] * prods_b[i + 1] * iFact[i] * iFact[rcnt] * dataset[i];
        }
    }
    return ret;
}

constexpr int fact_memo[]{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};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<mint> d(k + 10);
  rep(i, k + 10) d[i] = mint(i).pow(k);
  rep(i, k + 9) d[i+1] += d[i];
  mint ans = n * interpolate(d, n);
  rep(i, k + 10) d[i] = mint(i).pow(k + 1);
  rep(i, k + 9) d[i+1] += d[i];
  ans -= interpolate(d, n);
  ans *= 2;
  int t = n - 1;
  ans *= mint::raw(fact_memo[t / 10000000]);
  for (int i = t / 10000000 * 10000000 + 1; i <= t; i++) ans *= i;
  cout << ans.val() << '\n';
}
0