結果

問題 No.695 square1001 and Permutation 4
ユーザー veqccveqcc
提出日時 2019-09-01 16:52:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,831 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 106,884 KB
実行使用メモリ 81,216 KB
最終ジャッジ日時 2023-09-30 01:39:31
合計ジャッジ時間 5,816 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 WA -
testcase_02 AC 130 ms
22,864 KB
testcase_03 AC 58 ms
22,992 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
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>
using namespace std;
typedef pair <int, int> P;

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

// 拡張Euclidの互除法
// ap + bq = gcd(a, b) となるp,qを求め、return d = gcd(a,b)
int extGcd(int a, int b, int &p, int &q) {
    int 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 <int> &b, vector <int> &m) {
    int r = 0, M = 1;
    for (int i = 0; i < b.size(); i++) {
        int p, q;
        int d = extGcd(M, m[i], p, q);
        if ((b[i] - r) % d != 0) return {-1, -1};
        int 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 <int> MOD = {168647939, 592951213};
    vector <int> 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