結果

問題 No.950 行列累乗
ユーザー 👑 hitonanodehitonanode
提出日時 2019-12-14 00:29:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 13,964 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 202,904 KB
実行使用メモリ 14,604 KB
最終ジャッジ日時 2023-09-10 08:01:03
合計ジャッジ時間 24,896 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
4,620 KB
testcase_01 AC 11 ms
4,476 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 97 ms
14,276 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 23 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 32 ms
4,448 KB
testcase_11 AC 22 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 AC 16 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 19 ms
4,376 KB
testcase_20 AC 17 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2 ms
4,376 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,376 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2 ms
4,376 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
4,380 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 108 ms
14,516 KB
testcase_38 AC 114 ms
14,300 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 2 ms
4,376 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 2 ms
4,376 KB
testcase_46 WA -
testcase_47 AC 2 ms
4,376 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 AC 135 ms
14,312 KB
testcase_53 AC 146 ms
14,568 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 AC 14 ms
5,568 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 211 ms
14,316 KB
testcase_60 AC 289 ms
14,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
///// This part below is only for debug, not used /////
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
///// END /////


struct ModIntRuntime
{
    using lint = long long int;
    static int get_mod() { return mod; }
    int val;
    static int mod;
    static vector<ModIntRuntime> &facs()
    {
        static vector<ModIntRuntime> facs_;
        return facs_;
    }
    static int &get_primitive_root() {
        static int primitive_root_ = 0;
        if (!primitive_root_) {
            primitive_root_ = [&](){
                set<int> fac;
                int v = mod - 1;
                for (lint i = 2; i * i <= v; i++) while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < mod; g++) {
                    bool ok = true;
                    for (auto i : fac) if (ModIntRuntime(g).power((mod - 1) / i) == 1) { ok = false; break; }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root_;
    }
    static void set_mod(const int &m) {
        if (mod != m) facs().clear();
        mod = m;
        get_primitive_root() = 0;
    }
    ModIntRuntime &_setval(lint v) { val = (v >= mod ? v - mod : v); return *this; }
    ModIntRuntime() : val(0) {}
    ModIntRuntime(lint v) { _setval(v % mod + mod); }
    explicit operator bool() const { return val != 0; }
    ModIntRuntime operator+(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val + x.val); }
    ModIntRuntime operator-(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val - x.val + mod); }
    ModIntRuntime operator*(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val * x.val % mod); }
    ModIntRuntime operator/(const ModIntRuntime &x) const { return ModIntRuntime()._setval((lint)val * x.inv() % mod); }
    ModIntRuntime operator-() const { return ModIntRuntime()._setval(mod - val); }
    ModIntRuntime &operator+=(const ModIntRuntime &x) { return *this = *this + x; }
    ModIntRuntime &operator-=(const ModIntRuntime &x) { return *this = *this - x; }
    ModIntRuntime &operator*=(const ModIntRuntime &x) { return *this = *this * x; }
    ModIntRuntime &operator/=(const ModIntRuntime &x) { return *this = *this / x; }
    friend ModIntRuntime operator+(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod + x.val); }
    friend ModIntRuntime operator-(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod - x.val + mod); }
    friend ModIntRuntime operator*(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod * x.val % mod); }
    friend ModIntRuntime operator/(lint a, const ModIntRuntime &x) { return ModIntRuntime()._setval(a % mod * x.inv() % mod); }
    bool operator==(const ModIntRuntime &x) const { return val == x.val; }
    bool operator!=(const ModIntRuntime &x) const { return val != x.val; }
    bool operator<(const ModIntRuntime &x) const { return val < x.val; }
    friend istream &operator>>(istream &is, ModIntRuntime &x) { lint t; is >> t; x = ModIntRuntime(t); return is; }
    friend ostream &operator<<(ostream &os, const ModIntRuntime &x) { os << x.val;  return os; }
 
    lint power(lint n) const {
        lint ans = 1, tmp = this->val;
        while (n) {
            if (n & 1) ans = ans * tmp % mod;
            tmp = tmp * tmp % mod;
            n /= 2;
        }
        return ans;
    }
    lint inv() const { return this->power(mod - 2); }
    ModIntRuntime operator^(lint n) const { return ModIntRuntime(this->power(n)); }
    ModIntRuntime &operator^=(lint n) { return *this = *this ^ n; }
 
    ModIntRuntime fac() const {
        int l0 = facs().size();
        if (l0 > this->val) return facs()[this->val];
 
        facs().resize(this->val + 1);
        for (int i = l0; i <= this->val; i++) facs()[i] = (i == 0 ? ModIntRuntime(1) : facs()[i - 1] * ModIntRuntime(i));
        return facs()[this->val];
    }
 
    ModIntRuntime doublefac() const {
        lint k = (this->val + 1) / 2;
        if (this->val & 1) return ModIntRuntime(k * 2).fac() / ModIntRuntime(2).power(k) / ModIntRuntime(k).fac();
        else return ModIntRuntime(k).fac() * ModIntRuntime(2).power(k);
    }
 
    ModIntRuntime nCr(const ModIntRuntime &r) const {
        if (this->val < r.val) return ModIntRuntime(0);
        return this->fac() / ((*this - r).fac() * r.fac());
    }

    ModIntRuntime sqrt() const {
        if (val == 0) return 0;
        if (mod == 2) return val;
        if (power((mod - 1) / 2) != 1) return 0;
        ModIntRuntime b = 1;
        while (b.power((mod - 1) / 2) == 1) b += 1;
        int e = 0, m = mod - 1;
        while (m % 2 == 0) m >>= 1, e++;
        ModIntRuntime x = power((m - 1) / 2), y = (*this) * x * x;
        x *= (*this);
        ModIntRuntime z = b.power(m);
        while (y != 1) {
            int j = 0;
            ModIntRuntime t = y;
            while (t != 1) j++, t *= t;
            z = z.power(1LL << (e - j - 1));
            x *= z, z *= z, y *= z;
            e = j;
        }
        return ModIntRuntime(min(x.val, mod - x.val));
    }
};
int ModIntRuntime::mod = 1;


template<typename T>
T mod_determinant(vector<vector<T>> mtr)
{
    if (mtr.empty()) return 1;
    assert(mtr.size() == mtr[0].size());
    lint ans = 1;
    mtr = gauss_jordan(mtr);
    for (int i = 0; i < (int)mtr.size(); i++) ans = ans * mtr[i][i];
    return ans;
}

template<typename T>
vector<vector<T>> matmul(const vector<vector<T>> &A, const vector<vector<T>> &B)
{
    int H = A.size(), W = B[0].size(), K = B.size();
    vector<vector<T>> C(H, vector<T>(W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            for (int k = 0; k < K; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }
    return C;
}

template<typename T>
vector<T> matmul(const vector<vector<T>> &A, const vector<T> &v)
{
    vector<T> res(A.size());
    for (int i = 0; i < (int)A.size(); i++) {
        for (int j = 0; j < (int)v.size(); j++) {
            res[i] += A[i][j] * v[j];
        }
    }
    return res;
}

template<typename T>
vector<vector<T>> matpower(vector<vector<T>> X, lint n)
{
    vector<vector<T>> res(X.size(), vector<T>(X.size()));
    for (int i = 0; i < (int)res.size(); i++) res[i][i] = 1;
    while (n)
    {
        if (n & 1) res = matmul(res, X);
        X = matmul(X, X); n >>= 1;
    }
    return res;
}

template<typename T>
vector<T> eigen_vec_cand(vector<vector<T>> X, T a)
{
    X[0][0] -= a;
    X[1][1] -= a;
    return vector<T>{X[0][1], -X[0][0]};
}

using mint = ModIntRuntime;
using Matrix = vector<vector<mint>>;

template<typename T>
T eigval_cand(vector<vector<T>> M)
{
    // x^2 - 2ax - b = 0
    T a = (M[0][0] + M[1][1]) / 2;
    T b = M[0][1] * M[1][0] - M[0][0] * M[1][1];
    T u = b + a * a;
    T squ = u.sqrt();
    dbg(a);
    dbg(squ);
    return (a + squ == 0 ? a - squ : a + squ);
}

struct DiscreteLogarithm
{
    using lint = long long int;
    int M, stepsize;
    lint baby_a, giant_a, g;
    std::unordered_map<lint, int> baby_log_dict;

    lint inverse(lint a) {
        lint b = M / g, u = 1, v = 0;
        while (b) {
            lint t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        u %= M / g;
        return u >= 0 ? u : u + M / g;
    }

    DiscreteLogarithm(int mod, int a_new) : M(mod), baby_a(a_new % mod), giant_a(1) {
        g = 1;
        while (std::__gcd(baby_a, M / g) > 1) g *= std::__gcd(baby_a, M / g);
        stepsize = 32;  // lg(MAX_M)
        while (stepsize * stepsize < M / g) stepsize++;

        lint now = 1 % (M / g), inv_g = inverse(baby_a);
        for (int n = 0; n < stepsize; n++) {
            if (!baby_log_dict.count(now)) baby_log_dict[now] = n;
            (now *= baby_a) %= M / g;
            (giant_a *= inv_g) %= M / g;
        }
    }

    // log(): returns the smallest nonnegative x that satisfies a^x = b mod M, or -1 if there's no solution
    lint log(lint b) {
        b %= M;
        lint acc = 1 % M;
        for (int i = 0; i < stepsize; i++) {
            if (acc == b) return i;
            (acc *= baby_a) %= M;
        }
        if (b % g) return -1;  // No solution
        lint now = b * giant_a % (M / g);
        for (lint q = 1; q <= M / stepsize + 1; q++) {
            if (baby_log_dict.count(now)) return q * stepsize + baby_log_dict[now];
            (now *= giant_a) %= M / g;
        }
        return -1;
    }
};


void NO()
{
    puts("-1");
    exit(0);
}
bool valid_check(Matrix A, Matrix B)
{
    if (A[0][1] == 0 and B[0][1] != 0) NO();
    if (A[1][0] == 0 and B[1][0] != 0) NO();

    return true;
}

int main()
{
    int m;
    cin >> m;
    mint::set_mod(m);
    vector<vector<mint>> A(2, vector<mint>(2));
    vector<vector<mint>> B(2, vector<mint>(2));
    cin >> A >> B;

    FOR(i, 1, 100)
    {
        if (matpower(A, i) == B) {
            cout << i << endl;
            return 0;
        }
    }

    valid_check(A, B);

    if (A[0][1] == 0 and A[1][0] == 0)
    {
        // Aが対角行列:コーナーケース
        REP(d, 2) if ((A[d][d] == 0) ^ (B[d][d] == 0)) NO();

        lint n0 = DiscreteLogarithm(m, A[0][0].val).log(B[0][0].val);
        lint n1 = DiscreteLogarithm(m, A[1][1].val).log(B[1][1].val);
        if (A[0][0] == 0 and A[1][1] == 0) NO();
        else if (A[0][0] == 0)
        {
            cout << n1 << endl;
        }
        else if (A[1][1] == 0)
        {
            cout << n0 << endl;
        }
        else if (n0 < 0 or n1 < 0) NO();
        else
        {
            lint m0 = DiscreteLogarithm(m, A[0][0].val).log(1);
            lint t1 = DiscreteLogarithm(m, A[1][1].power(m0)).log((B[1][1] / A[1][1].power(n0)).val);
            if (t1 < 0) NO();
            else cout << n0 + m0 * t1 << endl;
        }
        return 0;
    }

    mint p;
    if (A[0][1]) p = B[0][1] / A[0][1];
    else p = B[1][0] / A[1][0];
    mint q = B[0][0] - p * A[0][0];

    if (A[1][1] * p + q != B[1][1] or A[1][0] * p != B[1][0] or A[0][1] * p != B[0][1]) NO();

    Matrix trans{{A[0][0] + A[1][1], 1}, {A[0][1] * A[1][0] - A[0][0] * A[1][1], 0}};
    if (trans[0][0] == 0 and trans[1][0] == 0) NO();

    Matrix trans_inv(2, vector<mint>(2));
    if (trans[1][0] != 0) trans_inv = {{0, 1 / trans[1][0]}, {1, trans[0][0] / trans[1][0]}};

    vector<mint> v{0, 1};
    map<vector<mint>, lint> ma;
    const lint BN = 100000;
    REP(d, BN)
    {
        if (!ma.count(v)) ma[v] = d;
        v = matmul(trans, v);
    }
    REP(e, BN)
    {
        vector<mint> tgt(2);
        if (trans[1][0] == 0)
        {
            tgt[0] = p / (mint(trans[0][0])^(BN * e));
        }
        else
        {
            tgt = matmul(matpower(trans_inv, e * BN), vector<mint>{p, q});
        }
        if (ma.count(tgt))
        {
            cout << ma[tgt] + e * BN << endl;
            return 0;
        }
    }
    NO();
}
0