Problem p03586 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n)                                                           \
  assert(n < 0x80000000);                                                      \
  for (int i = (a), i##_max = n; i < i##_max; ++i)
#define REM(i, a, n)                                                           \
  assert(n < 0x80000000);                                                      \
  for (int i = int(n) - 1, i##min = a; i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \
  {                                                                            \
    cin.tie(NULL);                                                             \
    ios::sync_with_stdio(false);                                               \
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
  return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}

#ifdef ONLY_MY_ENVIR
#include "Flow_Solver.h"
#include "Geometry.h"
#include "Global.h"
#include "Graph.h"
#include "IntMod.h"
#include "Matrix.h"
#include "Range.h"
#include "Rational.h"
#include "Segment_Tree.h"
#include "Suffix_Array.h"
#include "Tree.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>; // コスト小を優先
template <class T> auto Is(const T &value) {
  return [value](const auto &comparand) -> bool { return comparand == value; };
}
#endif

PP Extgcd(LL a, LL b) {
  if (b == 0)
    return PP(1, 0);
  const PP ref = Extgcd(b, a % b);
  const LL x = ref.first;
  const LL y = ref.second;
  return PP(y, x - (a / b) * y);
}

LL GCD(LL a, LL b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}

LL LCM(LL a, LL b) { return a / GCD(a, b) * b; }

map<LL, int> Factor(LL n) {
  map<LL, int> ret;
  for (LL i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      n /= i;
      ++ret[i];
    }
  }
  if (n != 1)
    ++ret[n];
  return ret;
}

LL Pow(LL base, LL exp) {
  if (exp == 0)
    return 1;
  return base * Pow(base, exp - 1);
}

LL Carmichael(LL n) {
  auto factors = Factor(n);
  LL lcm = 1;
  for (auto p : factors) {
    int base = p.first;
    int exp = p.second;

    LL tmp;
    if (base == 2) {
      tmp = (exp <= 2 ? exp : 1 << (exp - 2));
    } else {
      tmp = Pow(base, exp - 1) * (base - 1);
    }
    lcm = LCM(lcm, tmp);
  }
  return lcm;
}

LL Powmod(LL base, LL exp, LL mod) {
  if (exp == 0)
    return 1;
  LL tmp = Powmod(base, exp / 2, mod);
  LL tmp2 = tmp * tmp % mod;
  if (exp % 2 == 0) {
    return tmp2;
  } else {
    return tmp2 * base % mod;
  }
}

LL Sub(LL A, LL mod) {
  LL carm = Carmichael(mod);

  LL G = GCD(carm, mod);
  LL x = -1, y = -1;
  if (G < 100) {
    LL cnt = 0;
    LL prod = 1;
    while (cnt <= 200) {
      if ((prod - cnt) % G == 0) {
        y = cnt;
        x = prod;
        break;
      }
      prod = prod * A % mod;
      ++cnt;
    }
  } else {
    y = Sub(A, G);
    x = Powmod(A, y, mod);
  }
  x = x * Powmod(A, carm, mod) % mod;

  if (y == -1)
    throw 1;

  auto p = Extgcd(mod, carm);
  LL k = (x - y) / G * p.second % mod;
  LL ans = (k * carm + y) % (mod * carm);
  return ans > 0 ? ans : ans + mod * carm;
}

int main() {
  int Q;
  cin >> Q;
  REP(i, 0, Q) {
    int a, b;
    cin >> a >> b;
    LL test = Sub(a, b);
    cout << test << endl;
  }

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n)                                                           \
  assert(n < 0x80000000);                                                      \
  for (int i = (a), i##_max = n; i < i##_max; ++i)
#define REM(i, a, n)                                                           \
  assert(n < 0x80000000);                                                      \
  for (int i = int(n) - 1, i##min = a; i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \
  {                                                                            \
    cin.tie(NULL);                                                             \
    ios::sync_with_stdio(false);                                               \
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
  return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}

#ifdef ONLY_MY_ENVIR
#include "Flow_Solver.h"
#include "Geometry.h"
#include "Global.h"
#include "Graph.h"
#include "IntMod.h"
#include "Matrix.h"
#include "Range.h"
#include "Rational.h"
#include "Segment_Tree.h"
#include "Suffix_Array.h"
#include "Tree.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>; // コスト小を優先
template <class T> auto Is(const T &value) {
  return [value](const auto &comparand) -> bool { return comparand == value; };
}
#endif

PP Extgcd(LL a, LL b) {
  if (b == 0)
    return PP(1, 0);
  const PP ref = Extgcd(b, a % b);
  const LL x = ref.first;
  const LL y = ref.second;
  return PP(y, x - (a / b) * y);
}

LL GCD(LL a, LL b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}

LL LCM(LL a, LL b) { return a / GCD(a, b) * b; }

map<LL, int> Factor(LL n) {
  map<LL, int> ret;
  for (LL i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      n /= i;
      ++ret[i];
    }
  }
  if (n != 1)
    ++ret[n];
  return ret;
}

LL Pow(LL base, LL exp) {
  if (exp == 0)
    return 1;
  return base * Pow(base, exp - 1);
}

LL Carmichael(LL n) {
  auto factors = Factor(n);
  LL lcm = 1;
  for (auto p : factors) {
    int base = p.first;
    int exp = p.second;

    LL tmp;
    if (base == 2) {
      tmp = (exp <= 2 ? exp : 1 << (exp - 2));
    } else {
      tmp = Pow(base, exp - 1) * (base - 1);
    }
    lcm = LCM(lcm, tmp);
  }
  return lcm;
}

LL Powmod(LL base, LL exp, LL mod) {
  if (exp == 0)
    return 1;
  LL tmp = Powmod(base, exp / 2, mod);
  LL tmp2 = tmp * tmp % mod;
  if (exp % 2 == 0) {
    return tmp2;
  } else {
    return tmp2 * base % mod;
  }
}

LL Sub(LL A, LL mod) {
  LL carm = Carmichael(mod);

  LL G = GCD(carm, mod);
  LL x = -1, y = -1;
  if (carm < 100) {
    LL cnt = 0;
    LL prod = 1;
    while (cnt <= 200) {
      if ((prod - cnt) % G == 0) {
        y = cnt;
        x = prod;
        break;
      }
      prod = prod * A % mod;
      ++cnt;
    }
  } else {
    y = Sub(A, G);
    x = Powmod(A, y, mod);
  }
  x = x * Powmod(A, carm, mod) % mod;

  if (y == -1)
    throw 1;

  auto p = Extgcd(mod, carm);
  LL k = (x - y) / G * p.second % mod;
  LL ans = (k * carm + y) % (mod * carm);
  return ans > 0 ? ans : ans + mod * carm;
}

int main() {
  int Q;
  cin >> Q;
  REP(i, 0, Q) {
    int a, b;
    cin >> a >> b;
    LL test = Sub(a, b);
    cout << test << endl;
  }

  return 0;
}