結果

問題 No.3123 Inversion
ユーザー Kude
提出日時 2025-04-19 07:54:33
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 739 ms / 10,000 ms
コード長 2,096 bytes
コンパイル時間 3,456 ms
コンパイル使用メモリ 298,072 KB
実行使用メモリ 81,908 KB
最終ジャッジ日時 2025-04-19 07:55:00
合計ジャッジ時間 26,119 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

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 = modint;

constexpr int M = 5000010;
mint Fact[M], DFact[M], dp2[M], dp234[M];

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t, m;
  cin >> t >> m;
  mint::set_mod(m);
  Fact[0] = 1;
  for (int i = 1; i < M; i++) {
    Fact[i] = i * Fact[i-1];
  }
  DFact[0] = DFact[1] = 1;
  for (int i = 2; i < M; i++) {
    DFact[i] = i * DFact[i-2];
  }
  dp2[0] = dp2[1] = 1;
  for (int i = 2; i < M; i++) {
    dp2[i] = dp2[i-1] + (i - 1) * dp2[i-2];
  }
  dp234[0] = dp234[1] = 1;
  for (int i = 2; i < M; i++) {
    dp234[i] = 2 * dp234[i-2];
    if (i >= 4) dp234[i] += ((i - 2) & ~1) * dp234[i-4];
  }
  rep(_, t) {
    int n;
    cin >> n;
    if (n == 1) {
      cout << 1 % m << '\n';
      continue;
    }
    // def r:=rev, q=p^-1
    // {p,pr,rp,rpr,q,qr,rq,rqr} generated
    // 4 kinds of possible equalities:
    // 1. p=qr
    // 2. p=q
    // 3. p=rpr
    // 4. p=rqr
    // 1 <=> p^2=r => 3; p,p^3 generated
    // each 2,3,4 holds when the other two do
    mint c1, c2, c3, c4, c234, c0;
    if (n % 4 <= 1) {
      c1 = DFact[n / 2 - 1] * mint(2).pow(n / 4);
    }
    c2 = c4 = dp2[n];
    c3 = DFact[n & ~1];
    c234 = dp234[n];
    c2 -= c234, c3 -= c1 + c234, c4 -= c234;
    c0 = Fact[n] - c1 - c2 - c3 - c4 - c234;
    mint ans = 8 * c0 + 2 * c1 + 4 * (c2 + c3 + c4) + 2 * c234;
    cout << ans.val() << '\n';
  }
}
0