test/yosupo-judge/discrete_logarithm_mod/main.test.cpp
Depends on
Code
#define PROBLEM "https://judge.yosupo.jp/problem/discrete_logarithm_mod"
#include <iostream>
#include "Mylib/IO/input_tuples.cpp"
#include "Mylib/Number/Mod/mod_log.cpp"
namespace hl = haar_lib;
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int T;
std::cin >> T;
for (auto [X, Y, M] : hl::input_tuples<int, int, int>(T)) {
std::cout << hl::mod_log(X, Y, M).value_or(-1) << "\n";
}
return 0;
}
#line 1 "test/yosupo-judge/discrete_logarithm_mod/main.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/discrete_logarithm_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/Mod/mod_log.cpp"
#include <cmath>
#include <numeric>
#include <optional>
#include <unordered_map>
#line 2 "Mylib/Number/Mod/mod_inv.cpp"
#include <cstdint>
#line 4 "Mylib/Number/Mod/mod_inv.cpp"
namespace haar_lib {
constexpr int64_t mod_inv(int64_t a, int64_t m) {
int64_t b = m, u = 1, v = 0;
while (b) {
int64_t t = a / b;
a -= t * b;
a = a ^ b;
b = a ^ b;
a = a ^ b;
u -= t * v;
u = u ^ v;
v = u ^ v;
u = u ^ v;
}
u %= m;
if (u < 0) u += m;
return u;
}
} // namespace haar_lib
#line 3 "Mylib/Number/Mod/mod_pow.cpp"
namespace haar_lib {
constexpr int64_t mod_pow(int64_t n, int64_t p, int64_t m) {
int64_t ret = 1;
while (p > 0) {
if (p & 1) (ret *= n) %= m;
(n *= n) %= m;
p >>= 1;
}
return ret;
}
} // namespace haar_lib
#line 8 "Mylib/Number/Mod/mod_log.cpp"
namespace haar_lib {
std::optional<int64_t> mod_log(int64_t a, int64_t b, int64_t m) {
if (b == 1) return 0;
int64_t d = 0;
while (1) {
if (auto g = std::gcd(a, m); g != 1) {
if (b % g != 0) return {};
d += 1;
m /= g;
b /= g;
(b *= mod_inv(a / g, m)) %= m;
if (b == 1) return d;
} else {
break;
}
}
const int64_t sq = std::sqrt(m) + 1;
std::unordered_map<int64_t, int64_t> mp;
{
int64_t t = 1 % m;
for (int i = 0; i < sq; ++i) {
if (mp.find(t) == mp.end()) mp[t] = i;
(t *= a) %= m;
}
}
{
int64_t A = mod_pow(mod_inv(a, m), sq, m);
int64_t t = b % m;
for (int i = 0; i < sq; ++i) {
if (mp.find(t) != mp.end()) {
int64_t ret = i * sq + mp[t] + d;
return ret;
}
(t *= A) %= m;
}
}
return {};
}
} // namespace haar_lib
#line 6 "test/yosupo-judge/discrete_logarithm_mod/main.test.cpp"
namespace hl = haar_lib;
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int T;
std::cin >> T;
for (auto [X, Y, M] : hl::input_tuples<int, int, int>(T)) {
std::cout << hl::mod_log(X, Y, M).value_or(-1) << "\n";
}
return 0;
}
Back to top page