結果

問題 No.695 square1001 and Permutation 4
ユーザー PachicobuePachicobue
提出日時 2018-09-15 14:59:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,623 bytes
コンパイル時間 2,202 ms
コンパイル使用メモリ 203,284 KB
実行使用メモリ 81,224 KB
最終ジャッジ日時 2023-09-30 01:38:11
合計ジャッジ時間 4,758 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 17 ms
6,988 KB
testcase_02 AC 25 ms
22,600 KB
testcase_03 AC 21 ms
22,604 KB
testcase_04 AC 71 ms
22,584 KB
testcase_05 AC 75 ms
22,644 KB
testcase_06 AC 155 ms
42,052 KB
testcase_07 AC 137 ms
42,128 KB
testcase_08 AC 121 ms
42,008 KB
testcase_09 AC 155 ms
41,972 KB
testcase_10 AC 34 ms
11,112 KB
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
constexpr int MOD1 = 168647939;
constexpr int MOD2 = 592951213;
template <typename T>
constexpr std::pair<T, T> extgcd(const T a, const T b)
{
    if (b == 0) { return std::pair<T, T>{1, 0}; }
    const auto p = extgcd(b, a % b);
    return {p.second, p.first - p.second * (a / b)};
}
template <typename T>
std::pair<T, T> ChineseRemainderTheorem(const std::pair<T, T>& a1, const std::pair<T, T>& a2)  // (mod, value)
{
    const T p1 = a1.first, m1 = a1.second, p2 = a2.first, m2 = a2.second, m = p1 * p2;
    if (m1 == m2) { return {p1 * p2, m1}; }
    auto p = extgcd(p1, p2);
    return {m, (((p1 * p.first * (m2 - m1) + m1) % m) + m) % m};
}
int main()
{
    int N, M;
    std::cin >> N >> M;
    std::vector<int> x(M);
    for (int i = 0; i < M; i++) { std::cin >> x[i]; }
    const int H = (N + 1) / 2;
    std::vector<int> dp1(H, 0), dp2(H, 0);
    dp1[0] = dp2[0] = 1;
    for (int i = 1; i < H; i++) {
        for (int j = 0; j < M; j++) {
            if (i >= x[j]) {
                (dp1[i] += dp1[i - x[j]]) %= MOD1;
                (dp2[i] += dp2[i - x[j]]) %= MOD2;
            }
        }
    }
    ll ans1 = 0, ans2 = 0;
    for (int j = 0; j < M; j++) {
        for (int i = std::max(0, x[j] - H); i < std::min(N - H, x[j]); i++) {
            (ans1 += (ll)dp1[H - x[j] + i] * dp1[N - H - i - 1] % MOD1) %= MOD1;
            (ans2 += (ll)dp2[H - x[j] + i] * dp2[N - H - i - 1] % MOD2) %= MOD2;
        }
    }
    const auto ans = ChineseRemainderTheorem<__int128_t>({MOD1, ans1}, {MOD2, ans2});
    std::cout << (ll)ans.second << std::endl;
    return 0;
}
0