結果

問題 No.950 行列累乗
ユーザー 👑 hitonanodehitonanode
提出日時 2019-12-14 02:18:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 719 ms / 2,000 ms
コード長 14,078 bytes
コンパイル時間 3,188 ms
コンパイル使用メモリ 214,184 KB
実行使用メモリ 23,884 KB
最終ジャッジ日時 2023-09-10 10:01:40
合計ジャッジ時間 13,275 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
4,612 KB
testcase_01 AC 11 ms
4,468 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 41 ms
5,064 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 26 ms
4,376 KB
testcase_08 AC 44 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 33 ms
4,380 KB
testcase_11 AC 28 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 44 ms
4,380 KB
testcase_14 AC 52 ms
4,636 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 46 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 6 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 365 ms
23,884 KB
testcase_23 AC 44 ms
4,928 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 43 ms
4,456 KB
testcase_26 AC 46 ms
4,804 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 329 ms
23,696 KB
testcase_29 AC 296 ms
23,736 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 333 ms
23,672 KB
testcase_32 AC 339 ms
23,792 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 415 ms
23,668 KB
testcase_35 AC 403 ms
23,732 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 62 ms
9,596 KB
testcase_38 AC 65 ms
9,648 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 53 ms
6,024 KB
testcase_41 AC 50 ms
5,912 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 439 ms
23,736 KB
testcase_44 AC 499 ms
23,672 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 502 ms
23,696 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 507 ms
23,672 KB
testcase_49 AC 504 ms
23,668 KB
testcase_50 AC 711 ms
23,776 KB
testcase_51 AC 719 ms
23,684 KB
testcase_52 AC 96 ms
9,616 KB
testcase_53 AC 100 ms
9,664 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 14 ms
5,424 KB
testcase_57 AC 2 ms
4,388 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 175 ms
9,748 KB
testcase_60 AC 225 ms
9,876 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 determinant(vector<vector<T>> mtr)
{
    return mtr[0][0] * mtr[1][1] - mtr[0][1] * mtr[1][0];
}

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;
}


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


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;
}

Matrix inverse(Matrix m)
{
    mint det = determinant(m);
    return Matrix{{m[1][1] / det, -m[0][1] / det}, {-m[1][0] / det, m[0][0] / det}};
}

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 / A[0][0]).val) + 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 = (A[0][1] !=0 ? B[0][1] / A[0][1] : 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]) trans_inv = {{0, 1 / trans[1][0]}, {1, -trans[0][0] / trans[1][0]}};


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

    mint detA = determinant(A);
    lint i_det = DiscreteLogarithm(m, detA.val).log(determinant(B).val);
    if (i_det < 0) NO();
    lint p_det = DiscreteLogarithm(m, detA.val).log((1 / detA).val) + 1;
    map<Matrix, lint> ma;
    Matrix tmp = matpower(A, i_det);
    Matrix tmp_p = matpower(A, p_det);

    REP(i, BN + 2)
    {
        if (!ma.count(tmp) and i_det + p_det * i > 0) ma[tmp] = i_det + p_det * i;
        tmp = matmul(tmp, tmp_p);
    }
    REP(i, BN + 1)
    {
        Matrix q = matmul(inverse(matpower(tmp_p, i * BN)), B);
        if (ma.count(q))
        {
            cout << ma[q] + i * BN * p_det << endl;
            return 0;
        }
    }
    NO();
}
0