BZOJ3122: [Sdoi2013]隨機數生成器(BSGS)

自為風月馬前卒發表於2019-03-25

題意

題目連結

Sol

這題也比較休閒。

直接把\(X_{i+1} = (aX_i + b) \pmod P\)展開,推到最後會得到這麼個玩意兒

\[ a^{i-1} (x_1 + \frac{b}{a-1}) - \frac{b}{a-1} \equiv T \pmod P \]

然後再合併一下就可以大力BSGS了。

有些細節需要特判一下

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, INF = 1e9 + 10;;
int mod;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename A> A inv(A x) {return fp(x, mod - 2);}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int a, b, X1, End;
//x_{i+1} = (aX_i + b) % P
//a^ans = x % p
//a^{i * k - j} = x % p
//a^{i * k} = x * a^j % p
map<int, int> mp;

/*
int Query(int a, int x, int p) {
    if(__gcd(a, p) != 1) return -2;
    int base = 1;
    for(int i = 0; i <= p; i++) {
        if(base % p == x) return i;
        mul2(base, a);
    }
    return -2;
}
*/

int Query(int a, int x, int p) {
    if(__gcd(a, p) != 1) return -2;
    mp.clear(); int block = ceil(sqrt(p)), base = fp(a, block);
    for(int i = 0, cur = x; i <= block; i++, mul2(cur, a)) mp[cur] = i;
    for(int i = 1, cur = base; i <= block; i++, mul2(cur, base)) 
        if(mp[cur]) 
            return i * block - mp[cur];
    return -2;
}

void solve() {
    mod = read(); a = read(); b = read(); X1 = read(); End = read();
    if(X1 == End) {puts("1"); return ;}
    if(!a) {
        if(!b) {puts(End == X1 ? "1" : "-1");return ;}
        else {puts(End == b ? "2" : "-1");return ;}
    }
    if(a == 1) {
        if(!b) {puts(End == X1 ? "1" : "-1");return ;}
        else {
            //int tmp = add(End, -X1 + mod) % b;
            //cout << tmp << '\n';
            cout << mul(add(End, -X1), inv(b)) + 1 << '\n'; 
            return ;
        }
    }
    int tmp = mul(b, inv(a - 1));
    add2(X1, tmp); add2(End, tmp); 
    mul2(End, inv(X1));
    cout << Query(a, End, mod) + 1 << '\n';
}
signed main() {
    //freopen("a.in", "r", stdin);
    for(int T = read(); T--; solve());
    return 0;
}

相關文章