結果
問題 | No.1956 猫の額 |
ユーザー | 37zigen |
提出日時 | 2022-05-07 11:27:23 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 9,819 ms / 10,000 ms |
コード長 | 3,107 bytes |
コンパイル時間 | 3,399 ms |
コンパイル使用メモリ | 164,916 KB |
実行使用メモリ | 6,008 KB |
最終ジャッジ日時 | 2024-09-20 13:47:38 |
合計ジャッジ時間 | 75,196 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6,622 ms
5,872 KB |
testcase_01 | AC | 433 ms
5,992 KB |
testcase_02 | AC | 6,748 ms
5,880 KB |
testcase_03 | AC | 445 ms
5,996 KB |
testcase_04 | AC | 9,588 ms
5,752 KB |
testcase_05 | AC | 7,177 ms
5,996 KB |
testcase_06 | AC | 9,819 ms
5,996 KB |
testcase_07 | AC | 3,281 ms
6,000 KB |
testcase_08 | AC | 794 ms
5,376 KB |
testcase_09 | AC | 395 ms
5,660 KB |
testcase_10 | AC | 708 ms
5,376 KB |
testcase_11 | AC | 396 ms
5,376 KB |
testcase_12 | AC | 704 ms
5,376 KB |
testcase_13 | AC | 89 ms
5,376 KB |
testcase_14 | AC | 152 ms
5,376 KB |
testcase_15 | AC | 403 ms
5,660 KB |
testcase_16 | AC | 6,863 ms
5,588 KB |
testcase_17 | AC | 452 ms
6,000 KB |
testcase_18 | AC | 513 ms
6,008 KB |
testcase_19 | AC | 4,481 ms
6,008 KB |
testcase_20 | AC | 4,532 ms
6,000 KB |
コンパイルメッセージ
main.cpp: In function 'long long int primitive_root(long long int)': main.cpp:49:1: warning: control reaches end of non-void function [-Wreturn-type] 49 | } | ^
ソースコード
#include <algorithm> #include <cassert> #include <limits> #include <queue> #include <vector> #include <iostream> #include <assert.h> #include "atcoder/all" using namespace std; const int NMAX = 100; const int AMAX = 100000; vector<long long> A; int N, M, C, sumA; long long powmod(long long a, long long n, long long p) { if (n == 0) return 1; return powmod(a * a % p, n / 2, p) * (n % 2 == 1 ? a : 1) % p; } long long inv(long long a, long long p) { return powmod(a, p - 2, p); } bool is_prime(long long a) { for (long long div = 2; div * div <= a; ++div) if (a % div == 0) return false; return true; } long long primitive_root(long long p) { vector<long long> div; for (long long i = 2; i * i <= p - 1; ++i) { if ((p - 1) % i == 0) { div.push_back(i); div.push_back((p - 1) / i); } } for (long long i = 1; i < p - 1; ++i) { bool ok = true; for (long long d : div) { ok &= powmod(i, d, p) != 1; } if (ok) return i; } } long long garner(vector<int>& a, vector<int>& mods, long long m) { int n = a.size(); vector<int> b; auto gen_base = [&mods](int mod) { vector<int> base{ 1 }; for (int j = 0; j < mods.size(); ++j) { base.push_back((int)(1LL * base.back() * mods[j] % mod)); } return base; }; auto f = [&b](vector<int> base, int mod) { long long x = 0; for (int j = 0; j < b.size(); ++j) { x += 1LL * b[j] * base[j]; } x %= mod; return (int)x; }; for (int i = 0; i < n; ++i) { auto base = gen_base(mods[i]); b.push_back((int)(inv(base[i], mods[i]) * (-f(base, mods[i]) + a[i]) % mods[i])); if (b[i] < 0) b[i] += mods[i]; } return f(gen_base(m), m); } void solve(const long long mod, vector<int>& ans) { long long g = primitive_root(mod); long long r = powmod(g, (mod - 1) / C, mod); long long zeta = 1; for (int i = 0; i < C; ++i, zeta = zeta * r % mod) { vector<long long> f(ans.size()); f[0] = 1; int upper = 0; for (int a : A) { for (int j = upper; j >= 0; --j) if (f[j] != 0) { f[j + a] += zeta * f[j]; f[j + a] %= mod; } upper += a; } for (int j = 0; j < ans.size(); ++j) ans[j] += f[j]; } for (int i = 0; i < ans.size(); ++i) ans[i] %= mod; ans[0] -= C; long long invC = inv(C, mod); for (int i = 0; i < ans.size(); ++i) ans[i] = (int)(invC * ans[i] % mod); } int main() { cin >> N >> M >> C; A.resize(N); for (int i = 0; i < N; ++i) cin >> A[i]; for (int a : A) sumA += a; bool reversed = 2 * C < N; if (reversed) C = N - C; vector<int> mods; for (int m = 20'000'000 / C * C + 1; mods.size() != 4; m += C) { if (is_prime(m)) { mods.push_back(m); } } vector<vector<int>> x(4, vector<int>(sumA + 1)); for (int i = 0; i < mods.size(); ++i) { solve(mods[i], x[i]); } vector<int> ans(sumA + 1); for (int i = 0; i <= sumA; ++i) { vector<int> a; for (int j = 0; j < mods.size(); ++j) a.push_back(x[j][i]); ans[i] = garner(a, mods, M); } if (reversed) { for (int i = 0; i <= sumA / 2; ++i) { swap(ans[i], ans[sumA - i]); } } if (N == 2 * C) ans[sumA] -= 1; for (int i = 1; i <= sumA; ++i) printf("%d%c", ans[i], i == sumA ? '\n' : ' '); }