結果
| 問題 |
No.3123 Inversion
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-19 07:41:57 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 834 ms / 10,000 ms |
| コード長 | 2,597 bytes |
| コンパイル時間 | 3,406 ms |
| コンパイル使用メモリ | 277,528 KB |
| 実行使用メモリ | 70,652 KB |
| 最終ジャッジ日時 | 2025-04-19 07:42:25 |
| 合計ジャッジ時間 | 27,091 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
ソースコード
// written by ChatGPT o4-mini-high (really sorry)
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int T;
int 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]);
}
// mMax = floor(maxN/2)
int mMax = maxN / 2;
int bMax = mMax / 2; // for A(N)
// 1) factorial mod M: fact[n] = n! % M
vector<int> fact(maxN+1);
fact[0] = 1;
for(int i = 1; i <= maxN; i++){
fact[i] = int64(fact[i-1]) * i % M;
}
// 2) involutions count mod M: I(n)
// I(0)=I(1)=1, I(n)=I(n-1)+(n-1)*I(n-2)
vector<int> invCount(maxN+1);
invCount[0] = 1;
if(maxN >= 1) invCount[1] = 1;
for(int i = 2; i <= maxN; i++){
invCount[i] = (invCount[i-1] + int64(i-1) * invCount[i-2]) % M;
}
// 3) Fr(N) = (#P fixed by 180° 回転) = m! * 2^m (mod M)
// frArray[j] = Fr for N with floor(N/2)=j
vector<int> frArray(mMax+1);
frArray[0] = 1;
for(int j = 1; j <= mMax; j++){
// multiply by (2*j)
frArray[j] = int64(frArray[j-1]) * (2LL * j % M) % M;
}
// 4) H1(m): # of involutive P commuting with domain-反転 on m floor-pairs
// DP: H1[0]=1, H1[1]=2, for j>=2:
// H1[j] = 2*H1[j-1] + 2*(j-1)*H1[j-2]
vector<int> H1(mMax+1);
H1[0] = 1;
if(mMax >= 1) H1[1] = 2 % M;
for(int j = 2; j <= mMax; j++){
H1[j] = (2LL * H1[j-1] + 2LL * (j-1) * H1[j-2]) % M;
}
// 5) A(N) = #P fixed by 90° 回転 (order-4 回転)
// Let m = floor(N/2). If m is even, say m=2*k, then
// A(N) = (2*(2*1-1))*(2*(2*2-1))*···*(2*(2*k-1)) (mod M)
// B[0]=1; B[i]=B[i-1]*[2*(2*i-1)]
vector<int> B(bMax+1);
B[0] = 1;
for(int i = 1; i <= bMax; i++){
B[i] = int64(B[i-1]) * (2LL * (2LL*i - 1) % M) % M;
}
// 6) 出力
// sum_f(N) = 8*N! - 8*I(N) - 4*Fr(N) + 6*H1(m) - 2*A(N)
// 特殊に N=1 のとき 1
for(int N: Ns){
if(N == 1){
cout << 1 % M << "\n";
continue;
}
int m = N / 2;
int64 FN = fact[N];
int64 IN = invCount[N];
int64 FR = frArray[m];
int64 HN = H1[m];
int64 AN = (m % 2 == 0 ? B[m/2] : 0LL);
int64 ans = (
8LL * FN
- 8LL * IN
- 4LL * FR
+ 6LL * HN
- 2LL * AN
) % M;
if(ans < 0) ans += M;
cout << ans << "\n";
}
return 0;
}