結果

問題 No.695 square1001 and Permutation 4
ユーザー veqccveqcc
提出日時 2019-09-01 16:53:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,834 bytes
コンパイル時間 987 ms
コンパイル使用メモリ 108,580 KB
実行使用メモリ 81,424 KB
最終ジャッジ日時 2023-09-30 01:41:03
合計ジャッジ時間 12,541 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 115 ms
7,440 KB
testcase_02 AC 184 ms
22,820 KB
testcase_03 AC 126 ms
23,032 KB
testcase_04 AC 565 ms
22,804 KB
testcase_05 AC 490 ms
22,820 KB
testcase_06 AC 1,121 ms
42,172 KB
testcase_07 AC 1,124 ms
42,192 KB
testcase_08 AC 773 ms
42,144 KB
testcase_09 AC 829 ms
42,216 KB
testcase_10 AC 228 ms
11,228 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 <int> 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