This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/950
// competitive-verifier: TLE 1
// competitive-verifier: MLE 64
#include <iostream>
#include "src/Math/ModInt_Runtime.hpp"
#include "src/LinearAlgebra/Matrix.hpp"
#include "src/Math/DiscreteLogarithm.hpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
using Mint= ModInt_Runtime<int>;
using Mat= Matrix<Mint>;
int p;
cin >> p;
Mint::set_mod(p);
Mat A(2, 2), B(2, 2);
for (int i= 0; i < 2; ++i)
for (int j= 0; j < 2; ++j) cin >> A[i][j];
for (int i= 0; i < 2; ++i)
for (int j= 0; j < 2; ++j) cin >> B[i][j];
auto g= [](Mat a, Mat b) { return a * b; };
DiscreteLogarithm log1(g, g, [](Mat x) { return x[0][0].val(); }, 2ll * p + 1);
Mint detA= A[0][0] * A[1][1] - A[0][1] * A[1][0];
if (detA == Mint()) {
int ans= log1(A, A, B) + 1;
cout << (ans ? ans : -1) << '\n';
return 0;
}
Mint detB= B[0][0] * B[1][1] - B[0][1] * B[1][0];
auto f= [](Mint a, Mint b) { return a * b; };
DiscreteLogarithm log2(f, f, [](Mint x) { return x.val(); }, p);
int m= log2(detA, detA, 1) + 1, r= log2(detA, detA, detB) + 1;
if (m == 0 || r == 0) {
cout << -1 << '\n';
return 0;
}
long long q= log1(A.pow(m), A.pow(r), B);
cout << (q < 0 ? -1 : int64_t(m) * q + r) << '\n';
return 0;
}
#line 1 "test/yukicoder/950.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/950
// competitive-verifier: TLE 1
// competitive-verifier: MLE 64
#include <iostream>
#line 2 "src/Math/mod_inv.hpp"
#include <utility>
#include <type_traits>
#include <cassert>
template <class Uint> constexpr inline Uint mod_inv(Uint a, Uint mod) {
std::make_signed_t<Uint> x= 1, y= 0, z= 0;
for (Uint q= 0, b= mod, c= 0; b;) z= x, x= y, y= z - y * (q= a / b), c= a, a= b, b= c - b * q;
return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
#line 2 "src/Internal/Remainder.hpp"
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
struct MP_Na { // mod < 2^32
u32 mod;
constexpr MP_Na(): mod(0) {}
constexpr MP_Na(u32 m): mod(m) {}
constexpr inline u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
constexpr inline u32 set(u32 n) const { return n; }
constexpr inline u32 get(u32 n) const { return n; }
constexpr inline u32 norm(u32 n) const { return n; }
constexpr inline u32 plus(u64 l, u32 r) const { return l+= r, l < mod ? l : l - mod; }
constexpr inline u32 diff(u64 l, u32 r) const { return l-= r, l >> 63 ? l + mod : l; }
};
template <class u_t, class du_t, u8 B> struct MP_Mo { // mod < 2^32, mod < 2^62
u_t mod;
constexpr MP_Mo(): mod(0), iv(0), r2(0) {}
constexpr MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
constexpr inline u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
constexpr inline u_t set(u_t n) const { return mul(n, r2); }
constexpr inline u_t get(u_t n) const { return n= reduce(n), n >= mod ? n - mod : n; }
constexpr inline u_t norm(u_t n) const { return n >= mod ? n - mod : n; }
constexpr inline u_t plus(u_t l, u_t r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
constexpr inline u_t diff(u_t l, u_t r) const { return l-= r, l >> (B - 1) ? l + (mod << 1) : l; }
private:
u_t iv, r2;
static constexpr u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
constexpr inline u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
using MP_Mo32= MP_Mo<u32, u64, 32>;
using MP_Mo64= MP_Mo<u64, u128, 64>;
struct MP_Br { // 2^20 < mod <= 2^41
u64 mod;
constexpr MP_Br(): mod(0), x(0) {}
constexpr MP_Br(u64 m): mod(m), x((u128(1) << 84) / m) {}
constexpr inline u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
static constexpr inline u64 set(u64 n) { return n; }
constexpr inline u64 get(u64 n) const { return n >= mod ? n - mod : n; }
constexpr inline u64 norm(u64 n) const { return n >= mod ? n - mod : n; }
constexpr inline u64 plus(u64 l, u64 r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
constexpr inline u64 diff(u64 l, u64 r) const { return l-= r, l >> 63 ? l + (mod << 1) : l; }
private:
u64 x;
constexpr inline u128 quo(const u128 &n) const { return (n * x) >> 84; }
constexpr inline u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
template <class du_t, u8 B> struct MP_D2B1 { // mod < 2^63, mod < 2^64
u64 mod;
constexpr MP_D2B1(): mod(0), s(0), d(0), v(0) {}
constexpr MP_D2B1(u64 m): mod(m), s(__builtin_clzll(m)), d(m << s), v(u128(-1) / d) {}
constexpr inline u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
constexpr inline u64 set(u64 n) const { return n; }
constexpr inline u64 get(u64 n) const { return n; }
constexpr inline u64 norm(u64 n) const { return n; }
constexpr inline u64 plus(du_t l, u64 r) const { return l+= r, l < mod ? l : l - mod; }
constexpr inline u64 diff(du_t l, u64 r) const { return l-= r, l >> B ? l + mod : l; }
private:
u8 s;
u64 d, v;
constexpr inline u64 rem(const u128 &u) const {
u128 q= (u >> 64) * v + u;
u64 r= u64(u) - (q >> 64) * d - d;
if (r > u64(q)) r+= d;
if (r >= d) r-= d;
return r;
}
};
using MP_D2B1_1= MP_D2B1<u64, 63>;
using MP_D2B1_2= MP_D2B1<u128, 127>;
template <class u_t, class MP> constexpr u_t pow(u_t x, u64 k, const MP &md) {
for (u_t ret= md.set(1);; x= md.mul(x, x))
if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
}
#line 3 "src/Internal/modint_traits.hpp"
namespace math_internal {
struct m_b {};
struct s_b: m_b {};
}
template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;
template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;
#line 6 "src/Math/ModInt.hpp"
namespace math_internal {
template <class MP, u64 MOD> struct SB: s_b {
protected:
static constexpr MP md= MP(MOD);
};
template <class U, class B> struct MInt: public B {
using Uint= U;
static constexpr inline auto mod() { return B::md.mod; }
constexpr MInt(): x(0) {}
template <class T, typename= enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>>> constexpr MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
constexpr MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
constexpr MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
constexpr MInt name const { \
MInt ret; \
return ret.x= op, ret; \
}
FUNC(operator+(const MInt & r), B::md.plus(x, r.x))
FUNC(operator-(const MInt & r), B::md.diff(x, r.x))
FUNC(operator*(const MInt & r), B::md.mul(x, r.x))
FUNC(pow(u64 k), math_internal::pow(x, k, B::md))
#undef FUNC
constexpr MInt operator/(const MInt &r) const { return *this * r.inv(); }
constexpr MInt &operator+=(const MInt &r) { return *this= *this + r; }
constexpr MInt &operator-=(const MInt &r) { return *this= *this - r; }
constexpr MInt &operator*=(const MInt &r) { return *this= *this * r; }
constexpr MInt &operator/=(const MInt &r) { return *this= *this / r; }
constexpr bool operator==(const MInt &r) const { return B::md.norm(x) == B::md.norm(r.x); }
constexpr bool operator!=(const MInt &r) const { return !(*this == r); }
constexpr bool operator<(const MInt &r) const { return B::md.norm(x) < B::md.norm(r.x); }
constexpr inline MInt inv() const { return mod_inv<U>(val(), B::md.mod); }
constexpr inline Uint val() const { return B::md.get(x); }
friend ostream &operator<<(ostream &os, const MInt &r) { return os << r.val(); }
friend istream &operator>>(istream &is, MInt &r) {
i64 v;
return is >> v, r= MInt(v), is;
}
private:
Uint x;
};
template <u64 MOD> using MP_B= conditional_t < (MOD < (1 << 30)) & MOD, MP_Mo32, conditional_t < MOD < (1ull << 32), MP_Na, conditional_t<(MOD < (1ull << 62)) & MOD, MP_Mo64, conditional_t<MOD<(1ull << 41), MP_Br, conditional_t<MOD<(1ull << 63), MP_D2B1_1, MP_D2B1_2>>>>>;
template <u64 MOD> using ModInt= MInt < conditional_t<MOD<(1 << 30), u32, u64>, SB<MP_B<MOD>, MOD>>;
}
using math_internal::ModInt;
#line 3 "src/Math/ModInt_Runtime.hpp"
class Montgomery32 {}; // mod < 2^32 & mod is odd
class Montgomery64 {}; // mod < 2^62 & mod is odd
class Barrett {}; // 2^20 < mod <= 2^41
namespace math_internal {
struct r_b: m_b {};
}
template <class mod_t> constexpr bool is_runtimemodint_v= std::is_base_of_v<math_internal::r_b, mod_t>;
namespace math_internal {
template <class MP, u64 M, int id> struct RB: r_b {
static inline void set_mod(u64 m) { assert(m <= M), md= MP(m); }
static inline u64 max() { return M; }
protected:
static inline MP md;
};
template <class T, typename= enable_if_t<is_runtimemodint_v<T>>> constexpr u64 mv() { return T::max(); }
template <class Int, int id= -1> using ModInt_Runtime= conditional_t<is_same_v<Int, int>, MInt<u32, RB<MP_Na, u32(-1), id>>, conditional_t<is_same_v<Int, u32>, MInt<u32, RB<MP_Na, 0xFFFFFFFF, id>>, conditional_t<is_same_v<Int, long long>, MInt<u64, RB<MP_D2B1_1, (1ull << 63) - 1, id>>, conditional_t<is_same_v<Int, Montgomery32>, MInt<u32, RB<MP_Mo32, (1 << 30) - 1, id>>, conditional_t<is_same_v<Int, Montgomery64>, MInt<u64, RB<MP_Mo64, (1ull << 62) - 1, id>>, conditional_t<is_same_v<Int, Barrett>, MInt<u64, RB<MP_Br, 1ull << 41, id>>, MInt<u64, RB<MP_D2B1_2, u64(-1), id>>>>>>>>;
}
using math_internal::ModInt_Runtime;
#line 3 "src/LinearAlgebra/Matrix.hpp"
#include <vector>
#line 2 "src/LinearAlgebra/Vector.hpp"
#include <cstdint>
#line 4 "src/LinearAlgebra/Vector.hpp"
#include <valarray>
namespace _la_internal {
using namespace std;
template <class R> struct Vector {
valarray<R> dat;
Vector()= default;
Vector(size_t n): dat(n) {}
Vector(size_t n, const R &v): dat(v, n) {}
Vector(const initializer_list<R> &v): dat(v) {}
R &operator[](int i) { return dat[i]; }
const R &operator[](int i) const { return dat[i]; }
bool operator==(const Vector &r) const {
if (dat.size() != r.dat.size()) return false;
for (int i= dat.size(); i--;)
if (dat[i] != r.dat[i]) return false;
return true;
}
bool operator!=(const Vector &r) const { return !(*this == r); }
explicit operator bool() const { return dat.size(); }
Vector operator-() const { return Vector(dat.size())-= *this; }
Vector &operator+=(const Vector &r) { return dat+= r.dat, *this; }
Vector &operator-=(const Vector &r) { return dat-= r.dat, *this; }
Vector &operator*=(const R &r) { return dat*= r, *this; }
Vector operator+(const Vector &r) const { return Vector(*this)+= r; }
Vector operator-(const Vector &r) const { return Vector(*this)-= r; }
Vector operator*(const R &r) const { return Vector(*this)*= r; }
size_t size() const { return dat.size(); }
friend R dot(const Vector<R> &a, const Vector<R> &b) { return assert(a.size() == b.size()), (a.dat * b.dat).sum(); }
};
using u128= __uint128_t;
using u64= uint64_t;
using u8= uint8_t;
class Ref {
u128 *ref;
u8 i;
public:
Ref(u128 *ref, u8 i): ref(ref), i(i) {}
Ref &operator=(const Ref &r) { return *this= bool(r); }
Ref &operator=(bool b) { return *ref&= ~(u128(1) << i), *ref|= u128(b) << i, *this; }
Ref &operator|=(bool b) { return *ref|= u128(b) << i, *this; }
Ref &operator&=(bool b) { return *ref&= ~(u128(!b) << i), *this; }
Ref &operator^=(bool b) { return *ref^= u128(b) << i, *this; }
operator bool() const { return (*ref >> i) & 1; }
};
template <> class Vector<bool> {
size_t n;
public:
valarray<u128> dat;
Vector(): n(0) {}
Vector(size_t n): n(n), dat((n + 127) >> 7) {}
Vector(size_t n, bool b): n(n), dat(-u128(b), (n + 127) >> 7) {
if (int k= n & 127; k) dat[dat.size() - 1]&= (u128(1) << k) - 1;
}
Vector(const initializer_list<bool> &v): n(v.size()), dat((n + 127) >> 7) {
int i= 0;
for (bool b: v) dat[i >> 7]|= u128(b) << (i & 127), ++i;
}
Ref operator[](int i) { return {begin(dat) + (i >> 7), u8(i & 127)}; }
bool operator[](int i) const { return (dat[i >> 7] >> (i & 127)) & 1; }
bool operator==(const Vector &r) const {
if (dat.size() != r.dat.size()) return false;
for (int i= dat.size(); i--;)
if (dat[i] != r.dat[i]) return false;
return true;
}
bool operator!=(const Vector &r) const { return !(*this == r); }
explicit operator bool() const { return n; }
Vector operator-() const { return Vector(*this); }
Vector &operator+=(const Vector &r) { return dat^= r.dat, *this; }
Vector &operator-=(const Vector &r) { return dat^= r.dat, *this; }
Vector &operator*=(bool b) { return dat*= b, *this; }
Vector operator+(const Vector &r) const { return Vector(*this)+= r; }
Vector operator-(const Vector &r) const { return Vector(*this)-= r; }
Vector operator*(bool b) const { return Vector(*this)*= b; }
size_t size() const { return n; }
friend bool dot(const Vector<bool> &a, const Vector<bool> &b) {
assert(a.size() == b.size());
u128 v= 0;
for (int i= a.dat.size(); i--;) v^= a.dat[i] & b.dat[i];
return __builtin_parityll(v >> 64) ^ __builtin_parityll(u64(v));
}
};
template <class R> Vector<R> operator*(const R &r, const Vector<R> &v) { return v * r; }
template <class R> ostream &operator<<(ostream &os, const Vector<R> &v) {
os << '[';
for (int _= 0, __= v.size(); _ < __; ++_) os << (_ ? ", " : "") << v[_];
return os << ']';
}
}
using _la_internal::Vector;
#line 5 "src/LinearAlgebra/Matrix.hpp"
namespace _la_internal {
template <class R, class D> struct Mat {
Mat(): W(0) {}
Mat(size_t h, size_t w): W(w), dat(h * w) {}
Mat(size_t h, size_t w, R v): W(w), dat(v, h * w) {}
Mat(initializer_list<initializer_list<R>> v): W(v.size() ? v.begin()->size() : 0), dat(v.size() * W) {
auto it= begin(dat);
for (const auto &r: v) {
assert(r.size() == W);
for (R x: r) *it++= x;
}
}
size_t width() const { return W; }
size_t height() const { return W ? dat.size() / W : 0; }
auto operator[](int i) { return begin(dat) + i * W; }
auto operator[](int i) const { return begin(dat) + i * W; }
protected:
size_t W;
valarray<R> dat;
void add(const Mat &r) { assert(dat.size() == r.dat.size()), assert(W == r.W), dat+= r.dat; }
D mul(const Mat &r) const {
const size_t h= height(), w= r.W, l= W;
assert(l == r.height());
D ret(h, w);
auto a= begin(dat);
auto c= begin(ret.dat);
for (int i= h; i--; c+= w) {
auto b= begin(r.dat);
for (int k= l; k--; ++a) {
auto d= c;
auto v= *a;
for (int j= w; j--; ++b, ++d) *d+= v * *b;
}
}
return ret;
}
Vector<R> mul(const Vector<R> &r) const {
assert(W == r.size());
const size_t h= height();
Vector<R> ret(h);
auto a= begin(dat);
for (size_t i= 0; i < h; ++i)
for (size_t k= 0; k < W; ++k, ++a) ret[i]+= *a * r[k];
return ret;
}
};
template <class D> struct Mat<bool, D> {
struct Array {
u128 *bg;
Array(u128 *it): bg(it) {}
Ref operator[](int i) { return Ref{bg + (i >> 7), u8(i & 127)}; }
bool operator[](int i) const { return (bg[i >> 7] >> (i & 127)) & 1; }
};
struct ConstArray {
const u128 *bg;
ConstArray(const u128 *it): bg(it) {}
bool operator[](int i) const { return (bg[i >> 7] >> (i & 127)) & 1; }
};
Mat(): H(0), W(0), m(0) {}
Mat(size_t h, size_t w): H(h), W(w), m((w + 127) >> 7), dat(h * m) {}
Mat(size_t h, size_t w, bool b): H(h), W(w), m((w + 127) >> 7), dat(-u128(b), h * m) {
if (size_t i= h, k= w & 127; k)
for (u128 s= (u128(1) << k) - 1; i--;) dat[i * m]&= s;
}
Mat(const initializer_list<initializer_list<bool>> &v): H(v.size()), W(H ? v.begin()->size() : 0), m((W + 127) >> 7), dat(H * m) {
auto it= begin(dat);
for (const auto &r: v) {
assert(r.size() == W);
int i= 0;
for (bool b: r) it[i >> 7]|= u128(b) << (i & 127), ++i;
it+= m;
}
}
size_t width() const { return W; }
size_t height() const { return H; }
Array operator[](int i) { return {begin(dat) + i * m}; }
ConstArray operator[](int i) const { return {begin(dat) + i * m}; }
ConstArray get(int i) const { return {begin(dat) + i * m}; }
protected:
size_t H, W, m;
valarray<u128> dat;
void add(const Mat &r) { assert(H == r.H), assert(W == r.W), dat^= r.dat; }
D mul(const Mat &r) const {
assert(W == r.H);
D ret(H, r.W);
valarray<u128> tmp(r.m << 8);
auto y= begin(r.dat);
for (size_t l= 0; l < W; l+= 8) {
auto t= begin(tmp) + r.m;
for (int i= 0, n= min<size_t>(8, W - l); i < n; ++i, y+= r.m) {
auto u= begin(tmp);
for (int s= 1 << i; s--;) {
auto z= y;
for (int j= r.m; j--; ++u, ++t, ++z) *t= *u ^ *z;
}
}
auto a= begin(dat) + (l >> 7);
auto c= begin(ret.dat);
for (int i= H; i--; a+= m) {
auto u= begin(tmp) + ((*a >> (l & 127)) & 255) * r.m;
for (int j= r.m; j--; ++c, ++u) *c^= *u;
}
}
return ret;
}
Vector<bool> mul(const Vector<bool> &r) const {
assert(W == r.size());
Vector<bool> ret(H);
auto a= begin(dat);
for (size_t i= 0; i < H; ++i) {
u128 v= 0;
for (size_t j= 0; j < m; ++j, ++a) v^= *a & r.dat[j];
ret[i]= __builtin_parityll(v >> 64) ^ __builtin_parityll(u64(v));
}
return ret;
}
};
template <class R> struct Matrix: public Mat<R, Matrix<R>> {
using Mat<R, Matrix<R>>::Mat;
explicit operator bool() const { return this->W; }
static Matrix identity(int n) {
Matrix ret(n, n);
for (; n--;) ret[n][n]= R(true);
return ret;
}
Matrix submatrix(const vector<int> &rows, const vector<int> &cols) const {
Matrix ret(rows.size(), cols.size());
for (int i= rows.size(); i--;)
for (int j= cols.size(); j--;) ret[i][j]= (*this)[rows[i]][cols[j]];
return ret;
}
Matrix submatrix_rm(vector<int> rows, vector<int> cols) const {
sort(begin(rows), end(rows)), sort(begin(cols), end(cols)), rows.erase(unique(begin(rows), end(rows)), end(rows)), cols.erase(unique(begin(cols), end(cols)), end(cols));
const int H= this->height(), W= this->width(), n= rows.size(), m= cols.size();
vector<int> rs(H - n), cs(W - m);
for (int i= 0, j= 0, k= 0; i < H; ++i)
if (j < n && rows[j] == i) ++j;
else rs[k++]= i;
for (int i= 0, j= 0, k= 0; i < W; ++i)
if (j < m && cols[j] == i) ++j;
else cs[k++]= i;
return submatrix(rs, cs);
}
bool operator==(const Matrix &r) const {
if (this->width() != r.width() || this->height() != r.height()) return false;
for (int i= this->dat.size(); i--;)
if (this->dat[i] != r.dat[i]) return false;
return true;
}
bool operator!=(const Matrix &r) const { return !(*this == r); }
Matrix &operator*=(const Matrix &r) { return *this= this->mul(r); }
Matrix operator*(const Matrix &r) const { return this->mul(r); }
Matrix &operator*=(R r) { return this->dat*= r, *this; }
template <class T> Matrix operator*(T r) const {
static_assert(is_convertible_v<T, R>);
return Matrix(*this)*= r;
}
Matrix &operator+=(const Matrix &r) { return this->add(r), *this; }
Matrix operator+(const Matrix &r) const { return Matrix(*this)+= r; }
Vector<R> operator*(const Vector<R> &r) const { return this->mul(r); }
Vector<R> operator()(const Vector<R> &r) const { return this->mul(r); }
Matrix pow(uint64_t k) const {
size_t W= this->width();
assert(W == this->height());
for (Matrix ret= identity(W), b= *this;; b*= b)
if (k & 1 ? ret*= b, !(k>>= 1) : !(k>>= 1)) return ret;
}
};
template <class R, class T> Matrix<R> operator*(const T &r, const Matrix<R> &m) { return m * r; }
template <class R> ostream &operator<<(ostream &os, const Matrix<R> &m) {
os << "\n[";
for (int i= 0, h= m.height(); i < h; os << ']', ++i) {
if (i) os << "\n ";
os << '[';
for (int j= 0, w= m.width(); j < w; ++j) os << (j ? ", " : "") << m[i][j];
}
return os << ']';
}
template <class K> static bool is_zero(K x) {
if constexpr (is_floating_point_v<K>) return abs(x) < 1e-8;
else return x == K();
}
}
using _la_internal::Matrix;
#line 2 "src/Math/DiscreteLogarithm.hpp"
#include <cmath>
#line 3 "src/Internal/function_traits.hpp"
// clang-format off
namespace function_template_internal{
template<class C>struct is_function_object{
template<class U,int dummy=(&U::operator(),0)> static std::true_type check(U *);
static std::false_type check(...);
static C *m;
static constexpr bool value= decltype(check(m))::value;
};
template<class F,bool,bool>struct function_type_impl{using type= void;};
template<class F>struct function_type_impl<F,true,false>{using type= F *;};
template<class F>struct function_type_impl<F,false,true>{using type= decltype(&F::operator());};
template<class F> using function_type_t= typename function_type_impl<F,std::is_function_v<F>,is_function_object<F>::value>::type;
template<class... Args>struct result_type_impl{using type= void;};
template<class R,class... Args>struct result_type_impl<R(*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)const>{using type= R;};
template<class F> using result_type_t= typename result_type_impl<function_type_t<F>>::type;
template<class... Args>struct argument_type_impl{using type= void;};
template<class R,class... Args>struct argument_type_impl<R(*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)const>{using type= std::tuple<Args...>;};
template<class F> using argument_type_t= typename argument_type_impl<function_type_t<F>>::type;
}
using function_template_internal::result_type_t,function_template_internal::argument_type_t;
// clang-format on
#line 5 "src/Math/DiscreteLogarithm.hpp"
// mp : E × T -> T
// op : E × E -> E
// hash : T -> int
// s,t ∈ T, x ∈ E
// return min{ i : x^i(s) = t and i ∈ [0,N) } or -1 (not found)
template <class F, class G, class H> class DiscreteLogarithm {
const F ∓
const G &op;
const H &hash;
const int64_t lim;
using T= result_type_t<F>;
using E= result_type_t<G>;
public:
DiscreteLogarithm(const F &mp, const G &op, const H &hash, int64_t lim= 1ll << 50): mp(mp), op(op), hash(hash), lim(lim) { static_assert(std::is_convertible_v<std::invoke_result_t<H, T>, int>); }
int64_t operator()(const E &x, T s, const T &t, int64_t N= -1) const {
if (N < 0) N= lim;
const int m= 1 << std::__lg(int(std::sqrt(N) + 1)), mask= m - 1;
std::vector<T> val(m), vs(m);
std::vector<int> os(m + 1), so(m);
T s1= t;
for (int i= 0; i < m; ++i) ++os[so[i]= hash(val[i]= s1= mp(x, s1)) & mask];
for (int i= 0; i < m; ++i) os[i + 1]+= os[i];
for (int i= 0; i < m; ++i) vs[--os[so[i]]]= val[i];
E y= x;
for (int k= m; k>>= 1;) y= op(y, y);
bool failed= false;
for (int64_t n= 0;; s= s1) {
for (int a= hash(s1= mp(y, s)) & mask, j= os[a]; j < os[a + 1]; ++j) {
if (s1 == vs[j]) {
for (int i= 0;; s= mp(x, s)) {
if (s == t) return n + i < N ? n + i : -1;
if (++i == m) break;
}
if (failed) return -1;
failed= true;
break;
}
}
if ((n+= m) >= N) break;
}
return -1;
}
};
#line 8 "test/yukicoder/950.test.cpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
using Mint= ModInt_Runtime<int>;
using Mat= Matrix<Mint>;
int p;
cin >> p;
Mint::set_mod(p);
Mat A(2, 2), B(2, 2);
for (int i= 0; i < 2; ++i)
for (int j= 0; j < 2; ++j) cin >> A[i][j];
for (int i= 0; i < 2; ++i)
for (int j= 0; j < 2; ++j) cin >> B[i][j];
auto g= [](Mat a, Mat b) { return a * b; };
DiscreteLogarithm log1(g, g, [](Mat x) { return x[0][0].val(); }, 2ll * p + 1);
Mint detA= A[0][0] * A[1][1] - A[0][1] * A[1][0];
if (detA == Mint()) {
int ans= log1(A, A, B) + 1;
cout << (ans ? ans : -1) << '\n';
return 0;
}
Mint detB= B[0][0] * B[1][1] - B[0][1] * B[1][0];
auto f= [](Mint a, Mint b) { return a * b; };
DiscreteLogarithm log2(f, f, [](Mint x) { return x.val(); }, p);
int m= log2(detA, detA, 1) + 1, r= log2(detA, detA, detB) + 1;
if (m == 0 || r == 0) {
cout << -1 << '\n';
return 0;
}
long long q= log1(A.pow(m), A.pow(r), B);
cout << (q < 0 ? -1 : int64_t(m) * q + r) << '\n';
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | 99_challenge01.txt |
![]() |
18 ms | 7 MB |
g++-13 | 99_challenge02.txt |
![]() |
21 ms | 7 MB |
g++-13 | sample00.txt |
![]() |
7 ms | 4 MB |
g++-13 | sample01.txt |
![]() |
6 ms | 4 MB |
g++-13 | sample02.txt |
![]() |
6 ms | 4 MB |
g++-13 | sample03.txt |
![]() |
13 ms | 7 MB |
g++-13 | test00.txt |
![]() |
7 ms | 4 MB |
g++-13 | test01.txt |
![]() |
6 ms | 4 MB |
g++-13 | test02.txt |
![]() |
6 ms | 4 MB |
g++-13 | test03.txt |
![]() |
6 ms | 4 MB |
g++-13 | test04.txt |
![]() |
6 ms | 4 MB |
g++-13 | test05.txt |
![]() |
6 ms | 4 MB |
g++-13 | test06.txt |
![]() |
6 ms | 4 MB |
g++-13 | test07.txt |
![]() |
6 ms | 4 MB |
g++-13 | test08.txt |
![]() |
6 ms | 4 MB |
g++-13 | test09.txt |
![]() |
6 ms | 4 MB |
g++-13 | test10.txt |
![]() |
6 ms | 4 MB |
g++-13 | test11.txt |
![]() |
6 ms | 4 MB |
g++-13 | test12.txt |
![]() |
6 ms | 4 MB |
g++-13 | test13.txt |
![]() |
6 ms | 4 MB |
g++-13 | test14.txt |
![]() |
6 ms | 4 MB |
g++-13 | test15.txt |
![]() |
8 ms | 4 MB |
g++-13 | test16.txt |
![]() |
20 ms | 7 MB |
g++-13 | test17.txt |
![]() |
15 ms | 7 MB |
g++-13 | test18.txt |
![]() |
23 ms | 7 MB |
g++-13 | test19.txt |
![]() |
14 ms | 7 MB |
g++-13 | test20.txt |
![]() |
14 ms | 7 MB |
g++-13 | test21.txt |
![]() |
8 ms | 4 MB |
g++-13 | test22.txt |
![]() |
19 ms | 7 MB |
g++-13 | test23.txt |
![]() |
19 ms | 7 MB |
g++-13 | test24.txt |
![]() |
24 ms | 7 MB |
g++-13 | test25.txt |
![]() |
20 ms | 7 MB |
g++-13 | test26.txt |
![]() |
19 ms | 7 MB |
g++-13 | test27.txt |
![]() |
23 ms | 7 MB |
g++-13 | test28.txt |
![]() |
22 ms | 7 MB |
g++-13 | test29.txt |
![]() |
22 ms | 7 MB |
g++-13 | test30.txt |
![]() |
18 ms | 7 MB |
g++-13 | test31.txt |
![]() |
16 ms | 7 MB |
g++-13 | test32.txt |
![]() |
17 ms | 7 MB |
g++-13 | test33.txt |
![]() |
19 ms | 7 MB |
g++-13 | test34.txt |
![]() |
14 ms | 7 MB |
g++-13 | test35.txt |
![]() |
14 ms | 7 MB |
g++-13 | test36.txt |
![]() |
9 ms | 4 MB |
g++-13 | test37.txt |
![]() |
24 ms | 7 MB |
g++-13 | test38.txt |
![]() |
27 ms | 7 MB |
g++-13 | test39.txt |
![]() |
31 ms | 7 MB |
g++-13 | test40.txt |
![]() |
24 ms | 7 MB |
g++-13 | test41.txt |
![]() |
15 ms | 7 MB |
g++-13 | test42.txt |
![]() |
27 ms | 7 MB |
g++-13 | test43.txt |
![]() |
25 ms | 7 MB |
g++-13 | test44.txt |
![]() |
29 ms | 7 MB |
g++-13 | test45.txt |
![]() |
29 ms | 7 MB |
g++-13 | test46.txt |
![]() |
21 ms | 7 MB |
g++-13 | test47.txt |
![]() |
23 ms | 7 MB |
g++-13 | test48.txt |
![]() |
13 ms | 7 MB |
g++-13 | test49.txt |
![]() |
13 ms | 7 MB |
g++-13 | test50.txt |
![]() |
888 ms | 7 MB |
g++-13 | test51.txt |
![]() |
14 ms | 7 MB |
g++-13 | test52.txt |
![]() |
6 ms | 4 MB |
g++-13 | test53.txt |
![]() |
24 ms | 7 MB |
g++-13 | test54.txt |
![]() |
21 ms | 7 MB |
clang++-18 | 99_challenge01.txt |
![]() |
18 ms | 7 MB |
clang++-18 | 99_challenge02.txt |
![]() |
22 ms | 7 MB |
clang++-18 | sample00.txt |
![]() |
7 ms | 4 MB |
clang++-18 | sample01.txt |
![]() |
6 ms | 4 MB |
clang++-18 | sample02.txt |
![]() |
6 ms | 4 MB |
clang++-18 | sample03.txt |
![]() |
13 ms | 7 MB |
clang++-18 | test00.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test01.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test02.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test03.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test04.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test05.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test06.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test07.txt |
![]() |
6 ms | 4 MB |
clang++-18 | test08.txt |
![]() |
5 ms | 4 MB |
clang++-18 | test09.txt |
![]() |
5 ms | 4 MB |
clang++-18 | test10.txt |
![]() |
5 ms | 4 MB |
clang++-18 | test11.txt |
![]() |
5 ms | 4 MB |
clang++-18 | test12.txt |
![]() |
5 ms | 4 MB |
clang++-18 | test13.txt |
![]() |
5 ms | 4 MB |
clang++-18 | test14.txt |
![]() |
5 ms | 4 MB |
clang++-18 | test15.txt |
![]() |
8 ms | 4 MB |
clang++-18 | test16.txt |
![]() |
19 ms | 7 MB |
clang++-18 | test17.txt |
![]() |
15 ms | 7 MB |
clang++-18 | test18.txt |
![]() |
21 ms | 7 MB |
clang++-18 | test19.txt |
![]() |
15 ms | 7 MB |
clang++-18 | test20.txt |
![]() |
14 ms | 7 MB |
clang++-18 | test21.txt |
![]() |
8 ms | 4 MB |
clang++-18 | test22.txt |
![]() |
19 ms | 7 MB |
clang++-18 | test23.txt |
![]() |
21 ms | 7 MB |
clang++-18 | test24.txt |
![]() |
23 ms | 7 MB |
clang++-18 | test25.txt |
![]() |
21 ms | 7 MB |
clang++-18 | test26.txt |
![]() |
21 ms | 7 MB |
clang++-18 | test27.txt |
![]() |
24 ms | 7 MB |
clang++-18 | test28.txt |
![]() |
24 ms | 7 MB |
clang++-18 | test29.txt |
![]() |
22 ms | 7 MB |
clang++-18 | test30.txt |
![]() |
19 ms | 7 MB |
clang++-18 | test31.txt |
![]() |
18 ms | 7 MB |
clang++-18 | test32.txt |
![]() |
16 ms | 7 MB |
clang++-18 | test33.txt |
![]() |
20 ms | 7 MB |
clang++-18 | test34.txt |
![]() |
15 ms | 7 MB |
clang++-18 | test35.txt |
![]() |
14 ms | 7 MB |
clang++-18 | test36.txt |
![]() |
8 ms | 4 MB |
clang++-18 | test37.txt |
![]() |
24 ms | 7 MB |
clang++-18 | test38.txt |
![]() |
27 ms | 7 MB |
clang++-18 | test39.txt |
![]() |
31 ms | 7 MB |
clang++-18 | test40.txt |
![]() |
25 ms | 7 MB |
clang++-18 | test41.txt |
![]() |
14 ms | 7 MB |
clang++-18 | test42.txt |
![]() |
26 ms | 7 MB |
clang++-18 | test43.txt |
![]() |
26 ms | 7 MB |
clang++-18 | test44.txt |
![]() |
30 ms | 7 MB |
clang++-18 | test45.txt |
![]() |
30 ms | 7 MB |
clang++-18 | test46.txt |
![]() |
22 ms | 7 MB |
clang++-18 | test47.txt |
![]() |
24 ms | 7 MB |
clang++-18 | test48.txt |
![]() |
13 ms | 7 MB |
clang++-18 | test49.txt |
![]() |
13 ms | 7 MB |
clang++-18 | test50.txt |
![]() |
886 ms | 7 MB |
clang++-18 | test51.txt |
![]() |
15 ms | 7 MB |
clang++-18 | test52.txt |
![]() |
7 ms | 4 MB |
clang++-18 | test53.txt |
![]() |
26 ms | 7 MB |
clang++-18 | test54.txt |
![]() |
22 ms | 7 MB |