kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:x: test/yosupo-judge/tetration_mod/main.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/tetration_mod"

#include <iostream>
#include "Mylib/IO/input_tuples.cpp"
#include "Mylib/Number/tetration.cpp"

namespace hl = haar_lib;

int main() {
  int T;
  std::cin >> T;

  for (auto [A, B, M] : hl::input_tuples<int, int, int>(T)) {
    auto ans = hl::tetration(A, B, M);
    std::cout << ans << "\n";
  }

  return 0;
}
#line 1 "test/yosupo-judge/tetration_mod/main.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/tetration_mod"

#include <iostream>
#line 2 "Mylib/IO/input_tuples.cpp"
#include <initializer_list>
#line 4 "Mylib/IO/input_tuples.cpp"
#include <tuple>
#include <utility>
#include <vector>
#line 6 "Mylib/IO/input_tuple.cpp"

namespace haar_lib {
  template <typename T, size_t... I>
  static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) {
    (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...};
  }

  template <typename T, typename U>
  std::istream &operator>>(std::istream &s, std::pair<T, U> &value) {
    s >> value.first >> value.second;
    return s;
  }

  template <typename... Args>
  std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) {
    input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>());
    return s;
  }
}  // namespace haar_lib
#line 8 "Mylib/IO/input_tuples.cpp"

namespace haar_lib {
  template <typename... Args>
  class InputTuples {
    struct iter {
      using value_type = std::tuple<Args...>;
      value_type value;
      bool fetched = false;
      int N, c = 0;

      value_type operator*() {
        if (not fetched) {
          std::cin >> value;
        }
        return value;
      }

      void operator++() {
        ++c;
        fetched = false;
      }

      bool operator!=(iter &) const {
        return c < N;
      }

      iter(int N) : N(N) {}
    };

    int N;

  public:
    InputTuples(int N) : N(N) {}

    iter begin() const { return iter(N); }
    iter end() const { return iter(N); }
  };

  template <typename... Args>
  auto input_tuples(int N) {
    return InputTuples<Args...>(N);
  }
}  // namespace haar_lib
#line 2 "Mylib/Number/Totient/totient.cpp"
#include <cstdint>

namespace haar_lib {
  constexpr int64_t totient(int64_t n) {
    int64_t ret = n;

    for (int64_t i = 2; i * i <= n; ++i) {
      if (n % i == 0) {
        ret -= ret / i;
        while (n % i == 0) n /= i;
      }
    }

    if (n != 1) ret -= ret / n;

    return ret;
  }
}  // namespace haar_lib
#line 3 "Mylib/Number/tetration.cpp"

namespace haar_lib {
  namespace tetration_impl {
    int rec(int64_t a, int64_t b, int64_t m) {
      if (b == 1) return a % m;
      if (b == 0) return 1 % m;
      if (b == 2) {
        bool c      = a >= m;
        int64_t ret = 1;
        int64_t p   = a;
        a %= m;

        while (p > 0) {
          if (p & 1)
            if ((ret *= a) >= m) ret %= m, c = true;
          if ((a *= a) >= m) a %= m, c = true;
          p >>= 1;
        }

        if (c) ret += m;
        return ret;
      }
      if (a == 0) return b % 2 == 1 ? 0 : 1;
      if (m == 1) return 1;

      int phi = totient(m);
      int p   = rec(a, b - 1, phi);

      bool c      = p >= phi;
      int64_t ret = 1;

      while (p > 0) {
        if (p & 1)
          if ((ret *= a) >= m) ret %= m, c = true;
        if ((a *= a) >= m) a %= m, c = true;
        p >>= 1;
      }

      if (c) ret += m;
      return ret;
    }
  }  // namespace tetration_impl

  int tetration(int64_t a, int64_t b, int64_t m) {
    return tetration_impl::rec(a, b, m) % m;
  }
}  // namespace haar_lib
#line 6 "test/yosupo-judge/tetration_mod/main.test.cpp"

namespace hl = haar_lib;

int main() {
  int T;
  std::cin >> T;

  for (auto [A, B, M] : hl::input_tuples<int, int, int>(T)) {
    auto ans = hl::tetration(A, B, M);
    std::cout << ans << "\n";
  }

  return 0;
}
Back to top page