結果

問題 No.950 行列累乗
ユーザー 👑 hitonanodehitonanode
提出日時 2019-12-14 10:40:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 15,484 bytes
コンパイル時間 3,700 ms
コンパイル使用メモリ 212,352 KB
実行使用メモリ 14,544 KB
最終ジャッジ日時 2023-09-10 11:44:06
合計ジャッジ時間 10,642 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
4,564 KB
testcase_01 AC 11 ms
4,420 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 31 ms
4,884 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 16 ms
4,380 KB
testcase_08 AC 26 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 17 ms
4,380 KB
testcase_11 AC 16 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 26 ms
4,384 KB
testcase_14 AC 31 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 27 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 6 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 251 ms
14,280 KB
testcase_23 AC 34 ms
4,924 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 29 ms
4,496 KB
testcase_26 AC 35 ms
4,772 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 230 ms
14,520 KB
testcase_29 AC 197 ms
14,324 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 239 ms
14,340 KB
testcase_32 AC 245 ms
14,356 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 304 ms
14,300 KB
testcase_35 AC 296 ms
14,336 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 67 ms
9,668 KB
testcase_38 AC 70 ms
9,600 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 41 ms
5,912 KB
testcase_41 AC 41 ms
5,908 KB
testcase_42 AC 2 ms
4,384 KB
testcase_43 AC 312 ms
14,344 KB
testcase_44 AC 373 ms
14,304 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 370 ms
14,328 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 357 ms
14,292 KB
testcase_49 AC 370 ms
14,360 KB
testcase_50 AC 544 ms
14,544 KB
testcase_51 AC 548 ms
14,360 KB
testcase_52 AC 93 ms
9,588 KB
testcase_53 AC 100 ms
9,668 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 13 ms
5,560 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 1 ms
4,384 KB
testcase_59 AC 175 ms
9,608 KB
testcase_60 AC 225 ms
9,672 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 T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;


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>
struct matrix
{
    int H, W;
    std::vector<T> elem;
    typename std::vector<T>::iterator operator[](int i) { return elem.begin() + i * W; }
    inline T &at(int i, int j) { return elem[i * W + j]; }
    inline T get(int i, int j) const { return elem[i * W + j]; }
    operator std::vector<std::vector<T>>() const {
        std::vector<std::vector<T>> ret(H);
        for (int i = 0; i < H; i++) std::copy(elem.begin() + i * W, elem.begin() + (i + 1) * W, std::back_inserter(ret[i]));
        return ret;
    }

    matrix(int H = 0, int W = 0) : H(H), W(W), elem(H * W) {}
    matrix(const std::vector<std::vector<T>> &d) : H(d.size()), W(d.size() ? d[0].size() : 0) {
        for (auto &raw : d) std::copy(raw.begin(), raw.end(), std::back_inserter(elem));
    }

    static matrix Identity(int N) { 
        matrix ret(N, N);
        for (int i = 0; i < N; i++) ret.at(i, i) = 1;
        return ret;
    }

    matrix operator-() const { matrix ret(H, W); for (int i = 0; i < H * W; i++) ret.elem[i] = -elem[i]; return ret; }
    matrix operator+(const matrix &r) const { matrix ret(H, W); for (int i = 0; i < H * W; i++) ret.elem[i] += r.elem[i]; return ret; }
    matrix operator-(const matrix &r) const { matrix ret(H, W); for (int i = 0; i < H * W; i++) ret.elem[i] -= r.elem[i]; return ret; }
    matrix operator*(const matrix &r) const {
        matrix ret(H, r.W);
        for (int i = 0; i < H; i++) {
            for (int k = 0; k < W; k++) {
                for (int j = 0; j < r.W; j++) {
                    ret.at(i, j) += this->get(i, k) * r.get(k, j);
                }
            }
        } 
        return ret;
    }
    matrix &operator+=(const matrix &r) { return *this = *this + r; }
    matrix &operator-=(const matrix &r) { return *this = *this - r; }
    matrix &operator*=(const matrix &r) { return *this = *this * r; }
    bool operator==(const matrix &r) const { return H == r.H and W == r.W and elem == r.elem; }
    bool operator!=(const matrix &r) const { return H != r.H or W != r.W or elem != r.elem; }
    bool operator<(const matrix &r) const { return elem < r.elem; }
    matrix pow(int64_t n) const {
        matrix ret = Identity(H);
        if (n == 0) return ret;
        for (int i = 63 - __builtin_clzll(n); i >= 0; i--) {
            ret *= ret;
            if ((n >> i) & 1) ret *= (*this);
        }
        return ret;
    }
    matrix transpose() const {
        matrix ret(W, H);
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) ret.at(j, i) = this->get(i, j);
        return ret;
    }
    // Gauss-Jordan elimination
    // - Require inverse for every non-zero element
    // - Complexity: O(H^2 W)
    matrix gauss_jordan() const {
        int c = 0;
        matrix mtr(*this);
        for (int h = 0; h < H; h++) {
            if (c == W) break;
            int piv = -1;
            for (int j = h; j < H; j++) if (mtr.get(j, c)) {
                piv = j;
                break;
            }
            if (piv == -1) { c++; h--; continue; }
            if (h != piv) {
                for (int w = 0; w < W; w++) {
                    std::swap(mtr[piv][w], mtr[h][w]);
                    mtr.at(piv, w) *= -1; // To preserve sign of determinant
                }
            }
            for (int hh = h + 1; hh < H; hh++) {
                for (int w = W - 1; w >= c; w--) {
                    mtr.at(hh, w) -= mtr.at(h, w) * mtr.at(hh, c) * mtr.at(h, c).inv();
                }
            }
            c++;
        }
        return mtr;
    }
    int rank_of_gauss_jordan() const {
        for (int i = H * W - 1; i >= 0; i--) if (elem[i]) return i / W + 1;
        return 0;
    }
    T determinant_of_upper_triangle() const {
        T ret = 1;
        for (int i = 0; i < H; i++) ret *= get(i, i);
        return ret;
    }
    friend std::ostream &operator<<(std::ostream &os, const matrix &x) {
        os << "[(" << x.H << " * " << x.W << " matrix)";
        for (int i = 0; i < x.H; i++) {
            os << "\n[";
            for (int j = 0; j < x.W; j++) os << x.get(i, j) << ",";
            os << "]";
        }
        os << "]\n";
        return os;
    }
    friend std::istream &operator>>(std::istream &is, matrix &x) {
        for (auto &v : x.elem) is >> v;
        return is;
    }
};


using mint = ModIntRuntime;
using M = matrix<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(M A, M 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;
}

M inverse(M m)
{
    mint det = m.gauss_jordan().determinant_of_upper_triangle();
    return M({{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);
    M A(2, 2);
    M B(2, 2);
    cin >> A >> B;

    FOR(i, 1, 100) if (A.pow(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();

    M 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();

    M trans_inv(2, 2);
    if (trans[1][0]) trans_inv = M({{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 = A.gauss_jordan().determinant_of_upper_triangle();
    lint i_det = DiscreteLogarithm(m, detA.val).log(B.gauss_jordan().determinant_of_upper_triangle().val);
    if (i_det < 0) NO();
    lint p_det = DiscreteLogarithm(m, detA.val).log((1 / detA).val) + 1;
    map<M, lint> ma;
    M tmp = A.pow(i_det);
    M tmp_p = A.pow(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 = tmp * tmp_p;
    }
    REP(i, BN + 1)
    {
        M q = inverse(tmp_p.pow(i * BN)) * B;
        if (ma.count(q))
        {
            cout << ma[q] + i * BN * p_det << endl;
            return 0;
        }
    }
    NO();
}
0