比赛 2025.5.24 评测结果 AAAATTTTTT
题目名称 线性同余发生器 最终得分 40
用户昵称 LikableP 运行时间 5.999 s
代码语言 C++ 内存使用 1.52 MiB
提交时间 2025-05-24 10:00:40
显示代码纯文本
#include <cstdio>
typedef long long ll;

const ll MOD = 1e9 + 7;

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, x, y);
  ll z = x;
  x = y;
  y = z - (a / b) * y;
  return d;
}

ll a, m;
ll ans = 1;

int main() {
  freopen("LCG.in", "r", stdin);
  freopen("LCG.out", "w", stdout);
  scanf("%lld %lld", &a, &m);
  for (ll c = 1; c <= m - 1; ++c) {
    ll x, y;
    ll d = exgcd(a - 1, m, x, y);
    x = (x % m + m) % m - m;
    if (c % d) {
      printf("No black hole!\n");
      return 0;
    }
    //printf("c = %lld, x = %lld, d = %lld, x / d * -c = %lld\n", c, x, d, x / d * (-c));
    x = x / d * (-c) % m;
    ans = ans * x % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}