結果

問題 No.1025 Modular Equation
ユーザー ftiaschftiasch
提出日時 2020-04-14 21:44:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,431 ms / 5,000 ms
コード長 1,371 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 165,036 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 18:11:33
合計ジャッジ時間 53,605 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 8 ms
6,940 KB
testcase_04 AC 6 ms
6,940 KB
testcase_05 AC 5 ms
6,940 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 3,107 ms
6,944 KB
testcase_10 AC 3,209 ms
6,944 KB
testcase_11 AC 2,377 ms
6,944 KB
testcase_12 AC 2,286 ms
6,944 KB
testcase_13 AC 1,931 ms
6,940 KB
testcase_14 AC 1,949 ms
6,940 KB
testcase_15 AC 1,670 ms
6,940 KB
testcase_16 AC 1,589 ms
6,940 KB
testcase_17 AC 1,649 ms
6,940 KB
testcase_18 AC 1,549 ms
6,940 KB
testcase_19 AC 1,632 ms
6,940 KB
testcase_20 AC 1,586 ms
6,944 KB
testcase_21 AC 1,616 ms
6,944 KB
testcase_22 AC 1,628 ms
6,940 KB
testcase_23 AC 1,935 ms
6,944 KB
testcase_24 AC 3,431 ms
6,944 KB
testcase_25 AC 2,582 ms
6,944 KB
testcase_26 AC 2,307 ms
6,944 KB
testcase_27 AC 1,769 ms
6,944 KB
testcase_28 AC 1,734 ms
6,944 KB
testcase_29 AC 1,752 ms
6,940 KB
testcase_30 AC 1,704 ms
6,940 KB
testcase_31 AC 1,786 ms
6,940 KB
testcase_32 AC 2,054 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 1,680 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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]);
}
0