結果

問題 No.950 行列累乗
ユーザー PachicobuePachicobue
提出日時 2019-12-13 12:28:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 23,594 bytes
コンパイル時間 3,330 ms
コンパイル使用メモリ 251,236 KB
実行使用メモリ 12,616 KB
最終ジャッジ日時 2024-06-26 23:59:34
合計ジャッジ時間 12,364 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
6,912 KB
testcase_01 AC 56 ms
6,912 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 37 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
6,940 KB
testcase_16 WA -
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 50 ms
6,944 KB
testcase_22 AC 1,073 ms
11,704 KB
testcase_23 AC 81 ms
7,296 KB
testcase_24 AC 36 ms
6,940 KB
testcase_25 AC 68 ms
6,944 KB
testcase_26 AC 76 ms
7,424 KB
testcase_27 AC 38 ms
6,940 KB
testcase_28 AC 688 ms
11,232 KB
testcase_29 AC 760 ms
10,840 KB
testcase_30 AC 33 ms
6,944 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 33 ms
6,940 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 27 ms
6,944 KB
testcase_37 AC 50 ms
6,940 KB
testcase_38 AC 59 ms
6,944 KB
testcase_39 AC 30 ms
6,944 KB
testcase_40 AC 104 ms
7,808 KB
testcase_41 AC 85 ms
7,680 KB
testcase_42 AC 47 ms
6,940 KB
testcase_43 AC 959 ms
12,524 KB
testcase_44 AC 96 ms
7,936 KB
testcase_45 AC 48 ms
6,944 KB
testcase_46 AC 1,029 ms
12,616 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 137 ms
8,960 KB
testcase_49 AC 1,036 ms
12,520 KB
testcase_50 WA -
testcase_51 WA -
testcase_52 AC 101 ms
9,088 KB
testcase_53 AC 93 ms
9,088 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,944 KB
testcase_56 AC 59 ms
7,168 KB
testcase_57 AC 2 ms
6,944 KB
testcase_58 AC 2 ms
6,944 KB
testcase_59 AC 81 ms
8,320 KB
testcase_60 AC 68 ms
7,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// created [2019/12/12] 23:52:05
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"

using i32   = int32_t;
using i64   = int64_t;
using u32   = uint32_t;
using u64   = uint64_t;
using uint  = unsigned int;
using usize = std::size_t;
using ll    = long long;
using ull   = unsigned long long;
using ld    = long double;
template<typename T, usize n>
using arr = T (&)[n];
template<typename T, usize n>
using c_arr = const T (&)[n];
template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); }
template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); }
template<typename T> constexpr T msbp1(const T u) { return log2p1(u); }
template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); }
template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); }
template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; }
template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); }
template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); }
template<typename T> constexpr bool btest(const T mask, const usize ind) { return static_cast<bool>((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); }
template<typename T> void bset(T& mask, const usize ind) { mask |= (static_cast<T>(1) << ind); }
template<typename T> void breset(T& mask, const usize ind) { mask &= ~(static_cast<T>(1) << ind); }
template<typename T> void bflip(T& mask, const usize ind) { mask ^= (static_cast<T>(1) << ind); }
template<typename T> void bset(T& mask, const usize ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
constexpr unsigned int mod                  = 1000000007;
template<typename T> constexpr T inf_v      = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};

template<typename T>
T in()
{
    T v;
    return std::cin >> v, v;
}
template<typename T, typename Uint, usize n, usize i>
T in_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type) { return in<T>(); }
template<typename T, typename Uint, usize n, usize i>
auto in_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type& szs)
{
    const usize s = (usize)szs[i];
    std::vector<decltype(in_v<T, Uint, n, i + 1>(szs))> ans(s);
    for (usize j = 0; j < s; j++) { ans[j] = in_v<T, Uint, n, i + 1>(szs); }
    return ans;
}
template<typename T, typename Uint, usize n>
auto in_v(c_arr<Uint, n> szs) { return in_v<T, Uint, n, 0>(szs); }
template<typename... Types>
auto in_t() { return std::tuple<std::decay_t<Types>...>{in<Types>()...}; }
struct io_init
{
    io_init()
    {
        std::cin.tie(nullptr), std::ios::sync_with_stdio(false);
        std::cout << std::fixed << std::setprecision(20);
    }
} hogechan;
template<typename T>
void out(const T& v) { std::cout << v << '\n'; }  // Reactiveではstd::flushを忘れない
template<typename T, typename... Args>
void out(const T& v, const Args... args) { std::cout << v << ' ', out(args...); }
#    define SHOW(...) static_cast<void>(0)
constexpr ull TEN(const usize n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; }

template<typename T, typename Uint, usize n, usize i>
auto make_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type, const T& v = T{}) { return v; }
template<typename T, typename Uint, usize n, usize i>
auto make_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type szs, const T& v = T{})
{
    const usize s = (usize)szs[i];
    return std::vector<decltype(make_v<T, Uint, n, i + 1>(szs, v))>(s, make_v<T, Uint, n, i + 1>(szs, v));
}
template<typename T, typename Uint, usize n>
auto make_v(c_arr<Uint, n> szs, const T& t = T{}) { return make_v<T, Uint, n, 0>(szs, t); }


template<typename T> T gcd(const T& a, const T& b) { return a < 0 ? gcd(-a, b) : b < 0 ? gcd(a, -b) : (a > b ? gcd(b, a) : a == 0 ? b : gcd(b % a, a)); }
template<typename T> T lcm(const T& a, const T& b) { return a / gcd(a, b) * b; }
template<typename T>
constexpr std::pair<T, T> extgcd(const T a, const T b)
{
    if (b == 0) { return std::pair<T, T>{1, 0}; }
    const auto g = gcd(a, b), da = std::abs(b) / g;
    const auto p = extgcd(b, a % b);
    const auto x = (da + p.second % da) % da, y = (g - a * x) / b;
    return {x, y};
}
template<typename T>
constexpr T inverse(const T a, const T mod) { return extgcd(a, mod).first; }
template<uint mod_value, bool dynamic = false>
class modint_base
{
public:
    template<typename UInt = uint>
    static std::enable_if_t<dynamic, const UInt> mod() { return mod_ref(); }
    template<typename UInt = uint>
    static constexpr std::enable_if_t<not dynamic, const UInt> mod() { return mod_value; }
    template<typename UInt = uint>
    static void set_mod(const std::enable_if_t<dynamic, const UInt> mod) { mod_ref() = mod, inv_ref() = {1, 1}; }
    modint_base() : v{0} {}
    modint_base(const ll val) : v{norm(static_cast<uint>(val % static_cast<ll>(mod()) + static_cast<ll>(mod())))} {}
    modint_base(const modint_base& n) : v{n()} {}
    explicit operator bool() const { return v != 0; }
    bool operator!() const { return not static_cast<bool>(*this); }
    modint_base& operator=(const modint_base& m) { return v = m(), (*this); }
    modint_base& operator=(const ll val) { return v = norm(uint(val % static_cast<ll>(mod()) + static_cast<ll>(mod()))), (*this); }
    friend modint_base operator+(const modint_base& m) { return m; }
    friend modint_base operator-(const modint_base& m) { return make(norm(mod() - m.v)); }
    friend modint_base operator+(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + m2.v)); }
    friend modint_base operator-(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + mod() - m2.v)); }
    friend modint_base operator*(const modint_base& m1, const modint_base& m2) { return make(static_cast<uint>(static_cast<ll>(m1.v) * static_cast<ll>(m2.v) % static_cast<ll>(mod()))); }
    friend modint_base operator/(const modint_base& m1, const modint_base& m2) { return m1 * inv(m2.v); }
    friend modint_base operator+(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) + val}; }
    friend modint_base operator-(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) - val}; }
    friend modint_base operator*(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) * (val % static_cast<ll>(mod()))}; }
    friend modint_base operator/(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) * inv(val)}; }
    friend modint_base operator+(const ll val, const modint_base& m) { return modint_base{static_cast<ll>(m.v) + val}; }
    friend modint_base operator-(const ll val, const modint_base& m) { return modint_base{-static_cast<ll>(m.v) + val}; }
    friend modint_base operator*(const ll val, const modint_base& m) { return modint_base{static_cast<ll>(m.v) * (val % static_cast<ll>(mod()))}; }
    friend modint_base operator/(const ll val, const modint_base& m) { return modint_base{val * inv(static_cast<ll>(m.v))}; }
    friend modint_base& operator+=(modint_base& m1, const modint_base& m2) { return m1 = m1 + m2; }
    friend modint_base& operator-=(modint_base& m1, const modint_base& m2) { return m1 = m1 - m2; }
    friend modint_base& operator*=(modint_base& m1, const modint_base& m2) { return m1 = m1 * m2; }
    friend modint_base& operator/=(modint_base& m1, const modint_base& m2) { return m1 = m1 / m2; }
    friend modint_base& operator+=(modint_base& m, const ll val) { return m = m + val; }
    friend modint_base& operator-=(modint_base& m, const ll val) { return m = m - val; }
    friend modint_base& operator*=(modint_base& m, const ll val) { return m = m * val; }
    friend modint_base& operator/=(modint_base& m, const ll val) { return m = m / val; }
    friend modint_base operator^(const modint_base& m, const ll n) { return power(m.v, n); }
    friend modint_base& operator^=(modint_base& m, const ll n) { return m = m ^ n; }
    friend bool operator==(const modint_base& m1, const modint_base& m2) { return m1.v == m2.v; }
    friend bool operator!=(const modint_base& m1, const modint_base& m2) { return not(m1 == m2); }
    friend bool operator==(const modint_base& m, const ll val) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod()) + val % static_cast<ll>(mod()))); }
    friend bool operator!=(const modint_base& m, const ll val) { return not(m == val); }
    friend bool operator==(const ll val, const modint_base& m) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod()) + val % static_cast<ll>(mod()))); }
    friend bool operator!=(const ll val, const modint_base& m) { return not(m == val); }
    friend std::istream& operator>>(std::istream& is, modint_base& m)
    {
        ll v;
        return is >> v, m = v, is;
    }
    friend std::ostream& operator<<(std::ostream& os, const modint_base& m) { return os << m(); }
    uint operator()() const { return v; }
    static modint_base small_inv(const usize n)
    {
        auto& in = inv_ref();
        if (n < in.size()) { return in[n]; }
        for (usize i = in.size(); i <= n; i++) { in.push_back(-in[modint_base::mod() % i] * (modint_base::mod() / i)); }
        return in.back();
    }

private:
    template<typename UInt = uint>
    static std::enable_if_t<dynamic, UInt&> mod_ref()
    {
        static UInt mod = 0;
        return mod;
    }
    static uint norm(const uint x) { return x < mod() ? x : x - mod(); }
    static modint_base make(const uint x)
    {
        modint_base m;
        return m.v = x, m;
    }
    static modint_base power(modint_base x, ull n)
    {
        modint_base ans = 1;
        for (; n; n >>= 1, x *= x) {
            if (n & 1) { ans *= x; }
        }
        return ans;
    }
    static modint_base inv(const ll v) { return v < 1000000 ? small_inv(static_cast<usize>(v)) : modint_base{inverse(v, static_cast<ll>(mod()))}; }
    static std::vector<modint_base>& inv_ref()
    {
        static std::vector<modint_base> in{1, 1};
        return in;
    }
    uint v;
};
template<uint mod>
using modint = modint_base<mod, false>;
template<uint id>
using dynamic_modint = modint_base<id, true>;
template<typename T>
std::vector<T> divisors(const T n)
{
    std::vector<T> head, tail;
    for (T i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            head.push_back(i);
            if (i * i != n) { tail.push_back(n / i); }
        }
    }
    for (auto it = tail.rbegin(); it != tail.rend(); it++) { head.push_back(*it); }
    return head;
}

template<typename T>
std::vector<std::pair<T, usize>> prime_factors(T n)
{
    std::vector<std::pair<T, usize>> ans;
    for (T i = 2; i * i <= n; i++) {
        usize cnt = 0;
        for (; n % i == 0; n /= i, cnt++) {}
        if (cnt > 0) { ans.push_back({i, cnt}); }
    }
    if (n > 1) { ans.push_back({n, 1}); }
    return ans;
}
template<uint mod_value, bool dynamic>
ull period(const modint_base<mod_value, dynamic>& x)
{
    using mint    = modint_base<mod_value, dynamic>;
    const ull p   = mint::mod();  // constexprにしたいね
    const auto ds = divisors(p - 1);
    for (const ull d : ds) {
        if ((x ^ d) == 1) { return d; }
    }
    return p - 1;
}
template<typename mint>
mint primal_root()
{
    const ull p   = mint::mod();  // constexprにしたいね
    const auto fs = prime_factors(p - 1);
    mint g        = 1;
    for (;; g += 1) {
        bool ok = true;
        for (const auto& q : fs) {
            const ull per = (p - 1) / q.first;
            if ((g ^ per) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) { return g; }
    }
    return p;
}

template<typename T, typename InIt>
std::pair<T, T> crt(const InIt first, const InIt last)
{
    using P = std::pair<T, T>;
    return std::accumulate(first, last, P{0, 1}, [](const P& a1, const P& a2) -> P {
        T r1 = a1.first, m1 = a1.second, r2 = a2.first, m2 = a2.second;
        const T g = gcd(m1, m2);
        if (r1 % g != r2 % g) { return P{0, 0}; }
        const T m = m1 / g * m2;
        if (r1 == r2) { return {r1, m}; }
        const auto k1 = extgcd(m1, m2).first * ((r2 - r1) / g) % m;
        return P{(m + (__int128_t(m1) * k1 % m) + r1) % m, m};
    });
}

template<typename Ring>
class discrete_log
{
public:
    discrete_log(const Ring x, const ull period) : x{x}, period{period}
    {
        for (; bs * bs < period; bs++) {}
        for (ull i = 0; i * bs < period; i++) { giant[x ^ (i * bs)] = i * bs; }
    }
    ull operator()(Ring y)
    {
        for (ull i = 0; i < bs; i++) {
            if (giant.count(y)) { return (giant[y] + period - i) % period; }
            y = y * x;
        }
        return period;
    }

private:
    const Ring x;
    ull period;
    ull bs = 1;
    std::map<Ring, ull> giant;
};
using mint = dynamic_modint<0>;
bool operator<(const mint& m1, const mint& m2) { return m1() < m2(); }
mint g;
template<typename T = mint>
struct mat
{
    mat()
    {
        table[0][0] = T{0};
        table[0][1] = T{0};
        table[1][0] = T{0};
        table[1][1] = T{0};
    }
    mat(const T& a00, const T& a01, const T& a10, const T& a11)
    {
        table[0][0] = a00;
        table[0][1] = a01;
        table[1][0] = a10;
        table[1][1] = a11;
    }
    std::array<T, 2>& operator[](const int i) { return table[i]; }
    const std::array<T, 2>& operator[](const int i) const { return table[i]; }
    friend bool operator==(const mat& m1, const mat& m2) { return m1.table == m2.table; }
    friend mat operator+(const mat& m1, const mat& m2)
    {
        mat ans;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) { ans[i][j] = m1[i][j] + m2[i][j]; }
        }
        return ans;
    }
    friend mat operator-(const mat& m1, const mat& m2)
    {
        mat ans;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) { ans[i][j] = m1[i][j] - m2[i][j]; }
        }
        return ans;
    }
    friend mat operator*(const mat& m1, const mat& m2)
    {
        mat ans;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) { ans[i][j] = ans[i][j] + m1[i][k] * m2[k][j]; }
            }
        }
        return ans;
    }
    friend mat operator*(const mat& m, const T& v)
    {
        mat ans;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) { ans[i][j] = m[i][j] * v; }
        }
        return ans;
    }
    friend mat operator^(const mat& m, const ll k) { return k == 0 ? I() : k % 2 == 0 ? (m * m) ^ (k / 2) : (m ^ (k - 1)) * m; }
    friend std::ostream& operator<<(std::ostream& os, const mat& m)
    {
        os << "[";
        for (int i = 0; i < 2; i++) {
            os << "[";
            for (int j = 0; j < 2; j++) { os << m[i][j] << ","; }
            os << "]\n";
        }
        return os << "]\n";
    }
    static mat I()
    {
        mat ans;
        ans[0][0] = T{1}, ans[1][1] = T{1};
        return ans;
    }
    static T tr(const mat& m) { return m[0][0] + m[1][1]; }
    static T det(const mat& m) { return m[0][0] * m[1][1] - m[0][1] * m[1][0]; }
    static mat inv(const mat& m)
    {
        const T d = det(m);
        mat<T> ans;
        ans[0][0] = m[1][1] / d;
        ans[0][1] = -m[0][1] / d;
        ans[1][0] = -m[1][0] / d;
        ans[1][1] = m[0][0] / d;
        return ans;
    }
    std::array<std::array<T, 2>, 2> table;
};

struct C
{
    C() : x{0}, y{0} {}
    C(const mint& x) : x{x}, y{0} {}
    C(const mint& x, const mint& y) : x{x}, y{y} {}
    friend bool operator==(const C& c1, const C& c2) { return c1.x == c2.x and c1.y == c2.y; }
    friend bool operator!=(const C& c1, const C& c2) { return not(c1 == c2); }
    friend bool operator<(const C& c1, const C& c2) { return c1.x != c2.x ? c1.x < c2.x : c1.y < c2.y; }
    friend C operator-(const C& c) { return C{-c.x, -c.y}; }
    friend C operator+(const C& c1, const C& c2) { return C{c1.x + c2.x, c1.y + c2.y}; }
    friend C operator-(const C& c1, const C& c2) { return C{c1.x - c2.x, c1.y - c2.y}; }
    friend C operator*(const C& c1, const C& c2) { return C{c1.x * c2.x + g * c1.y * c2.y, c1.x * c2.y + c1.y * c2.x}; }
    friend C operator/(const C& c1, const C& c2) { return c1 * conj(c2); }
    friend C operator*(const C& c, const mint& v) { return C{c.x * v, c.y * v}; }
    friend C operator/(const C& c, const mint& v) { return C{c.x / v, c.y / v}; }

    friend C operator^(const C& c, const ll k)
    {
        const mint a = c.x;
        const mint b = c.y;
        const auto m = (mat<mint>(a, b * g, b, a)) ^ k;
        return C{m[0][0], m[1][0]};
    }

    static mint normSq(const C& c) { return c.x * c.x - g * c.y * c.y; }
    static C conj(const C& c) { return C{c.x, -c.y} / normSq(c); }
    friend std::ostream& operator<<(std::ostream& os, const C& c) { return os << c.x << "+" << c.y << "\\sqrt(g)"; }
    mint x, y;  // x+y\sqrt{g}
};

int main()
{
    const uint p = in<uint>();
    mint::set_mod(p);
    g             = primal_root<mint>();
    const auto as = in_v<mint>({4});
    const auto bs = in_v<mint>({4});
    mat<mint> A(as[0], as[1], as[2], as[3]);
    mat<mint> B(bs[0], bs[1], bs[2], bs[3]);
    for (int i = 1; i <= 100; i++) {
        if ((A ^ i) == B) { return std::cout << i << std::endl, 0; }
    }
    discrete_log<mint> pr_dlog(g, p - 1);
    auto sqrt = [&](const mint x) -> C {
        if (x == 0) { return {0, 0}; }
        const ll y = pr_dlog(x);
        if (y % 2 == 0) {
            return {g ^ (y / 2), 0};
        } else {
            return {0, g ^ ((y - 1) / 2)};
        }
    };
    auto eigens = [&](const mat<mint>& A) -> std::array<C, 2> {
        const auto s  = mat<mint>::tr(A);
        const auto t  = mat<mint>::det(A);
        const auto d  = s * s - 4 * t;
        const auto sq = sqrt(d);
        return {(C{s, 0} + sq) / 2, (C{s, 0} - sq) / 2};
    };
    auto eigen_vecs = [&](const auto& A) -> mat<C> {  // D=S^(-1)ASのS
        if (A[0][1] == 0 and A[1][0] == 0) { return mat<C>(C{1}, C{0}, C{0}, C{1}); }
        const auto es = eigens(A);
        if (es[0] == es[1]) {  // ジョルダン標準形
            const mint l = es[0].x;
            const auto N = A - mat<mint>::I() * l;
            SHOW(N);
            if (N[0][1] != 0 or N[1][1] != 0) {
                return mat<C>(C{N[0][1]}, C{0}, C{N[1][1]}, C{1});
            } else {
                return mat<C>(C{N[0][0]}, C{1}, C{N[1][0]}, C{0});
            }
        } else {  // 対角化可能
            std::array<std::array<C, 2>, 2> ans;
            for (int i = 0; i < 2; i++) {
                std::array<C, 2> v1{{C{A[0][0], 0} - es[i], C{A[0][1], 0}}};
                if (v1[0] == C{} and v1[1] == C{}) {
                    v1 = {{C{A[1][0], 0}, C{A[1][1], 0} - es[i]}};
                }
                SHOW(i, v1);
                ans[i] = {{-v1[1], v1[0]}};
            }
            return mat<C>(ans[0][0], ans[1][0], ans[0][1], ans[1][1]);
        }
    };
    auto normal = [&](const C& c) {
        if (c == C{0}) { return c; }
        if (c.y == 0) { return C{1, 0}; }
        return C{c.x / c.y, 1};
    };
    SHOW(eigens(A));
    SHOW(eigens(B));
    const auto AS = eigen_vecs(A);
    SHOW(AS);
    mat<C> CA(C{A[0][0]}, C{A[0][1]}, C{A[1][0]}, C{A[1][1]});
    mat<C> CB(C{B[0][0]}, C{B[0][1]}, C{B[1][0]}, C{B[1][1]});
    CA = mat<C>::inv(AS) * CA * AS;
    CB = mat<C>::inv(AS) * CB * AS;
    SHOW(CA, CB);  // ここまでが間違っていたらマジで終了
    auto lg = [&](const C& c1, const C& c2) -> std::pair<ll, ll> {
        // c=a+b\sqrt{g}について
        // c^(p+1) = a^2-(b^2)g (特にF_pの元)
        if (c1 == C{0}) {
            if (c2 == C{0}) {
                return {0, 1};
            } else {
                return {0, 0};
            }
        }
        if (c2 == C{0}) { return {0, 0}; }
        static std::vector<uint> ds;
        if (ds.empty()) { ds = divisors(p + 1); }
        ll per = p + 1;
        for (const ll d : ds) {
            if ((c1 ^ d).y == 0) {
                per = d;
                break;
            }
        }
        const mint c = c2.x;
        const mint d = c2.y;
        const mint n = (c1 ^ per).x;
        assert(n != 0);
        SHOW(n);
        if (d == 0) {
            const ull per2 = period(n);
            const ull lg2  = discrete_log<mint>(n, per2)(c);
            return {lg2 * per, per * per2};
        } else {
            auto res = [&]() {
                ll bs = 1;
                for (; bs * bs < per; bs++) {}
                std::map<C, ll> giant;
                for (ll i = 0; i * bs < per; i++) {
                    const C n = normal(c1 ^ (i * bs));
                    giant[n]  = i * bs;
                }
                C c2_ = normal(c2);
                for (ll i = 0; i < bs; i++) {
                    if (giant.count(c2_)) { return (per + giant[c2_] - i) % per; }
                    c2_ = normal(c2_ * c1);
                }
                return per;
            }();
            SHOW(res, per);
            if (res == per) { return {0, 0}; }
            const C cr = c1 ^ res;
            const C d  = c2 / cr;
            SHOW(d);
            assert(d.y == 0);
            const mint r   = d.x;
            const ull per2 = period(n);
            const ull lg2  = discrete_log<mint>(n, per2)(r);
            return {lg2 * per + res, per2 * per};
        }
    };

    if (CB[1][0] != C{0}) { return std::cout << -1 << std::endl, 0; }
    if (CA[0][1] == C{0}) {  // 対角
        if (CB[0][1] != C{0}) { return std::cout << -1 << std::endl, 0; }
        const auto p1 = lg(CA[0][0], CB[0][0]);
        const auto p2 = lg(CA[1][1], CB[1][1]);
        if (p1.second == 0 or p2.second == 0) { return std::cout << -1 << std::endl, 0; }
        std::vector<std::pair<ll, ll>> mods{p1, p2};
        SHOW(mods);
        const auto ans = crt<ll>(mods.begin(), mods.end());
        ll n           = ans.first;
        if (n == 0) { n = ans.second; }
        std::cout << ((A ^ n) == B ? n : -1LL) << std::endl;
    } else {  // 非対角
        if (CB[1][0] == C{0}) { return std::cout << -1 << std::endl, 0; }
        if (CB[0][0] != CB[1][1]) { return std::cout << -1 << std::endl, 0; }
        const mint l  = CA[0][0].x;
        const mint m  = (CB[0][1].x * l) / CB[0][0].x;
        const auto p1 = lg(CA[0][0], CB[0][0]);
        const auto p2 = std::pair<ll, ll>{(ll)m(), (ll)p};
        if (p1.second == 0 or p2.second == 0) { return std::cout << -1 << std::endl, 0; }
        std::vector<std::pair<ll, ll>> mods{p1, p2};
        SHOW(mods);
        const auto ans = crt<ll>(mods.begin(), mods.end());
        SHOW(ans);
        ll n = ans.first;
        if (n == 0) { n = ans.second; }
        std::cout << ((A ^ n) == B ? n : -1LL) << std::endl;
    }
    return 0;
}
0