結果

問題 No.3123 Inversion
ユーザー Mistletoe
提出日時 2025-04-19 15:33:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,152 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 74,156 KB
実行使用メモリ 46,388 KB
最終ジャッジ日時 2025-04-19 15:34:13
合計ジャッジ時間 13,108 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other AC * 2 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

const int MAX_N = 5e6;

vector<long long> factorial(MAX_N + 1);

void precompute_factorials(int N, long long M) {
    factorial[0] = 1;
    for (int i = 1; i <= N; ++i) {
        factorial[i] = (factorial[i-1] * i) % M;
    }
}

long long solve_case(int N, long long M) {
    if (N == 1) {
        return 1 % M;
    }
    long long total_permutations = factorial[N];
    long long palindromic;
    if (N % 2 == 0) {
        palindromic = factorial[N / 2];
    } else {
        palindromic = factorial[(N - 1) / 2];
    }
    long long sum = (total_permutations + palindromic) % M;
    return sum;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    long long M;
    cin >> T >> M;

    int max_N = 0;
    vector<int> test_cases(T);
    for (int i = 0; i < T; ++i) {
        cin >> test_cases[i];
        if (test_cases[i] > max_N) {
            max_N = test_cases[i];
        }
    }

    precompute_factorials(max_N, M);

    for (int i = 0; i < T; ++i) {
        int N = test_cases[i];
        cout << solve_case(N, M) << '\n';
    }

    return 0;
}
0