Mr. Panda and Kakin(拓展欧几里得 + O(1)快速乘)

Mr. Panda and Kakin

给定 n , c n, c n,c,要我们找到 n n n是两个相邻质数的乘积,要我们找到 x x x,满足 x 2 30 + 3 ≡ c ( m o d n ) x ^{2 ^{30} + 3} \equiv c \pmod n x230+3c(modn) 1 0 10 ≤ n ≤ 1 0 18 , 0 < c < n 10 ^{10} \leq n \leq 10 ^ {18}, 0 < c < n 1010n1018,0<c<n

考虑得到 2 30 + 3 2 ^{30} + 3 230+3 ϕ ( n ) \phi(n) ϕ(n)下的逆元,为 i n v = ( 2 30 + 3 ) ϕ ( n ) − 1 inv = (2 ^{30} + 3) ^{\phi(n) - 1} inv=(230+3)ϕ(n)1,则有 c i n v ≡ x ( 2 30 + 3 ) i n v ≡ x ( m o d n ) c ^{inv} \equiv x ^{(2^{30} + 3)inv} \equiv x \pmod{n} cinvx(230+3)invx(modn),使用 O ( 1 ) O(1) O(1)快速乘即可。

#include <bits/stdc++.h>

using namespace std;

long long n, c, phi, inv;

inline long long mul(long long x, long long y, long long mod) {
  return (x * y - (long long)((long double)x / mod * y) * mod + mod) % mod;
}

long long exgcd(long long a, long long b, long long & x, long long & y) {
  if(!b) {
      x = 1, y = 0;s
      return a;
  }
  long long gcd = exgcd(b, a % b, x, y);
  long long temp = x;
  x = y;
  y = temp - a / b * y;
  return gcd;
}

long long quick_pow(long long a, long long n, long long mod) {
  long long ans = 1;
  while (n) {
    if (n & 1) {
      ans = mul(ans, a, mod);
    }
    a = mul(a, a, mod);
    n >>= 1;
  }
  return ans;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  int T, cas = 0;
  scanf("%d", &T);
  while (T--) {
    scanf("%lld %lld", &n, &c);
    long long p = sqrt(n), q;
    while (true) {
      if (n % p == 0) {
        break;
      }
      p--;
    }
    q = n / p;
    phi = (p - 1) * (q - 1);
    exgcd((1ll << 30) + 3, phi, inv, p);
    inv = ((inv % phi) + phi) % phi;
    printf("Case %d: %lld\n", ++cas, quick_pow(c, inv, n));
  }
  return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值