結果
問題 | No.1025 Modular Equation |
ユーザー |
![]() |
提出日時 | 2020-04-14 21:44:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3,563 ms / 5,000 ms |
コード長 | 1,371 bytes |
コンパイル時間 | 1,768 ms |
コンパイル使用メモリ | 167,000 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-01 18:12:21 |
合計ジャッジ時間 | 54,607 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 32 |
ソースコード
#include <bits/stdc++.h>const int N = 500;const int P = 100000;const int MOD = 1e9 + 7;void add(int &x, int a) {x += a;if (x >= MOD) {x -= MOD;}}int p, n, k, b, dp[2][P];int power(int a, int n) {int res = 1;while (n) {if (n & 1) {res = 1ULL * res * a % p;}a = 1ULL * a * a % p;n >>= 1;}return res;}bool is_primitive_root(int g) {for (int d = 2; d * d <= p - 1; ++d) {if ((p - 1) % d == 0 && (power(g, d) == 1 || power(g, (p - 1) / d) == 1)) {return false;}}return true;}int main() {scanf("%d%d%d%d", &p, &n, &k, &b);k = std::__gcd(k, p - 1);int g = p < 3 ? 1 : 2;while (!is_primitive_root(g)) {g++;}int gk = power(g, k);dp[0][0] = 1;for (int i = 0, a; i < n; ++i) {scanf("%d", &a);int x = 1;for (int j = 0; j <= k; ++j) {int sum = dp[i & 1][x];for (int t = 0, y = p - 1; t < (p - 1) / k; ++t) {add(sum, static_cast<uint64_t>(k) *dp[i & 1][(x + static_cast<uint64_t>(a) * y) % p] % MOD);y = static_cast<uint64_t>(y) * gk % p;}for (int t = 0, y = x; t < (p - 1) / k; ++t) {dp[i + 1 & 1][y] = sum;y = static_cast<uint64_t>(y) * gk % p;}x = j + 1 < k ? static_cast<uint64_t>(x) * g % p : 0;}}printf("%d\n", dp[n & 1][b]);}