結果
問題 |
No.3123 Inversion
|
ユーザー |
|
提出日時 | 2025-04-19 13:29:13 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,175 bytes |
コンパイル時間 | 2,894 ms |
コンパイル使用メモリ | 198,584 KB |
実行使用メモリ | 134,292 KB |
最終ジャッジ日時 | 2025-04-19 13:29:41 |
合計ジャッジ時間 | 24,880 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 1 |
other | AC * 18 WA * 3 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; ll M; cin >> T >> M; vector<int> Ns(T); int maxN = 0; for(int i = 0; i < T; i++){ cin >> Ns[i]; maxN = max(maxN, Ns[i]); } // 1) factorial mod M vector<ll> fact(maxN+1); fact[0] = 1; for(int i = 1; i <= maxN; i++){ fact[i] = fact[i-1] * i % M; } // 2) involutions a[n]: a[0]=1, a[1]=1, a[n]=a[n-1]+(n-1)*a[n-2] vector<ll> a(maxN+1); a[0] = 1; if(maxN >= 1) a[1] = 1; for(int n = 2; n <= maxN; n++){ a[n] = (a[n-1] + (ll)(n-1) * a[n-2]) % M; } // 3) pow2 up to floor(maxN/2) for c[n] int halfN = maxN/2; vector<ll> pow2(halfN+1); pow2[0] = 1; for(int i = 1; i <= halfN; i++){ pow2[i] = (pow2[i-1] * 2) % M; } // 4) P[m] = product_{k=1..m}(4k-2) mod M for d[n] int quarterN = maxN/4; vector<ll> P(quarterN+1); P[0] = 1; for(int m = 1; m <= quarterN; m++){ // 4*m - 2 fits in ll P[m] = (P[m-1] * (4LL*m - 2)) % M; } // 5) H[m] = number of involutions that are also 180°-symmetric on floor(n/2) pairs // H[0]=1, H[1]=2, H[m]=2*H[m-1] + 2*(m-1)*H[m-2] vector<ll> H(halfN+1); H[0] = 1; if(halfN >= 1) H[1] = 2 % M; for(int m = 2; m <= halfN; m++){ H[m] = (2 * H[m-1] + 2LL*(m-1) * H[m-2]) % M; } // 6) 答えを計算 // c[n] = fact[floor(n/2)] * pow2[floor(n/2)] // d[n] = (n%4==0 || n%4==1) ? P[floor(n/4)] : 0 // h[n] = H[floor(n/2)] // S[n] = 8*fact[n] - 8*a[n] - 4*c[n] - 2*d[n] + 6*h[n] (mod M) // for(int n : Ns){ ll fn = fact[n]; ll an = a[n]; int m2 = n/2; ll cn = (fact[m2] * pow2[m2]) % M; int m4 = n/4; ll dn = (n%4==0 || n%4==1) ? P[m4] : 0; ll hn = H[m2]; ll S = 0; S = (S + 8LL * fn) % M; S = (S - 8LL * an % M + M) % M; S = (S - 4LL * cn % M + M) % M; S = (S - 2LL * dn % M + M) % M; S = (S + 6LL * hn) % M; cout << S << "\n"; } return 0; }