マトリックスマトリックスBSGS bzoj4128

トピックポータル

https://lydsy.com/JudgeOnline/problem.php?id=4128

問題の解決策

私は、任意のアイデアなしで10分だと思います。

そして、一見言葉"のデータは、ことを確認するために、\(p個の\を)内に解ける"、および\(p型\のLeq 19997 \)を ...

だから、この質問は、行列の事を置き換え合同クラスのBSGSの数ではありません。

質問は二つの行列が等しいかどうかを判断するどのように迅速です。ハッシュ聖歌。

そして、何もそこではありません。


それはBSGSの逆バージョンを記述する必要はありません要件を推奨します。

#include<bits/stdc++.h>
#include<tr1/unordered_map>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 70 + 7;
const int base = 1997;

int n, P;
std::tr1::unordered_map<ull, int> mp;

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
    return ans;
}

struct Matrix {
    int a[N][N];
    
    inline Matrix() { memset(a, 0, sizeof(a)); }
    inline Matrix(const int &x) {
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= n; ++i) a[i][i] = x;
    }
    
    inline Matrix operator * (const Matrix &b) {
        Matrix c;
        for (int k = 1; k <= n; ++k)
            for (int i = 1; i <= n; ++i)
                for (int j = 1; j <= n; ++j) sadd(c.a[i][j], (ll)a[i][k] * b.a[k][j] % P);
        return c;
    }
    inline ull hash() {
        ull ha = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                ha = ha * base + (a[i][j] + 1);
        return ha;
    }
} A, B;

inline Matrix fpow(Matrix x, int y) {
    Matrix ans(1);
    for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
    return ans;
}

inline int bsgs() {
    int m = sqrt(P);
    Matrix C = B, e;
    for (int i = 0; i < m; ++i, C = C * A) mp[C.hash()] = i;
    e = C = fpow(A, m);
    for (int i = 1; ; ++i, C = C * e) if (mp.count(C.hash())) return i * m - mp[C.hash()];
}

inline void work() {
    printf("%d\n", bsgs());
}

inline void init() {
    read(n), read(P);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            read(A.a[i][j]);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            read(B.a[i][j]);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

おすすめ

転載: www.cnblogs.com/hankeke/p/bzoj4128.html