結果

問題 No.695 square1001 and Permutation 4
ユーザー veqccveqcc
提出日時 2019-09-01 16:50:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,833 bytes
コンパイル時間 1,077 ms
コンパイル使用メモリ 110,832 KB
実行使用メモリ 159,620 KB
最終ジャッジ日時 2023-09-30 01:40:24
合計ジャッジ時間 12,975 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 116 ms
11,176 KB
testcase_02 AC 189 ms
42,108 KB
testcase_03 AC 135 ms
42,240 KB
testcase_04 AC 568 ms
42,228 KB
testcase_05 AC 497 ms
42,132 KB
testcase_06 MLE -
testcase_07 MLE -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 AC 228 ms
19,108 KB
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
typedef pair <ll, ll> P;

ll mod(ll a, ll m) {
    return (a % m + m) % m;
}

// 拡張Euclidの互除法
// ap + bq = gcd(a, b) となるp,qを求め、return d = gcd(a,b)
ll extGcd(ll a, ll b, ll &p, ll &q) {
    ll d = a;
    if (b == 0) {
        p = 1;
        q = 0;
    } else {
        d = extGcd(b, a % b, q, p);
        q -= (a / b) * p;
    }
    return d;
}

// Chinese Remainder Theorem
// 解あり -> x = r mod m なら return (r, m)
// 解なし -> return (-1, -1)
P CRT(vector <ll> &b, vector <ll> &m) {
    ll r = 0, M = 1;
    for (int i = 0; i < b.size(); i++) {
        ll p, q;
        ll d = extGcd(M, m[i], p, q);
        if ((b[i] - r) % d != 0) return {-1, -1};
        ll tmp = (b[i] - r) / d * p % (m[i] / d); // s = (b1 - b2) / d
        r += M * tmp; // x = r + s * M * p の形
        M *= m[i] / d; // mod を lcm に更新
    }
    return {mod(r, M), M};
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;

    vector <int> x(m);
    for (int i = 0; i < m; i++) cin >> x[i];

    vector <ll> MOD = {168647939, 592951213};
    vector <ll> b(2);
    for (int i = 0; i < 2; i++) {
        vector <ll> dp(n);
        dp[0] = 1;
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < m; k++) {
                if (j + x[k] < n) {
                    (dp[j + x[k]] += dp[j]) %= MOD[i];
                }
            }
        }
        b[i] = dp[n - 1];
    }

    P p = CRT(b, MOD);
    cout << p.first << "\n";
    return 0;
}
0