专栏首页数据结构与算法BZOJ3122: [Sdoi2013]随机数生成器(BSGS)

BZOJ3122: [Sdoi2013]随机数生成器(BSGS)

题意

题目链接

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)

    多读读题就会发现题目要求的就是可相交的最小路径覆盖,那么按照套路先floyd一遍,如果能联通的话就再二分图中加边,然后判一下最大匹配数就行了。刚开始以为因为有的...

    attack
  • loj#6030. 「雅礼集训 2017 Day1」矩阵(贪心 构造)

    attack
  • 洛谷P4360 [CEOI2004]锯木厂选址(dp 斜率优化)

    attack
  • 洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)

    多读读题就会发现题目要求的就是可相交的最小路径覆盖,那么按照套路先floyd一遍,如果能联通的话就再二分图中加边,然后判一下最大匹配数就行了。刚开始以为因为有的...

    attack
  • BZOJ1802: [Ahoi2009]checker(性质分析 dp)

    attack
  • cf121C. Lucky Permutation(康托展开)

    由于阶乘的数量增长非常迅速,而\(k\)又非常小,那么显然最后的序列只有最后几位会发生改变。

    attack
  • 洛谷P4360 [CEOI2004]锯木厂选址(dp 斜率优化)

    attack
  • 洛谷P3199 [HNOI2009]最小圈(01分数规划)

    attack
  • 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

    设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\)。可以二分一个答案\(k\),我们需要检查\(\sum...

    attack
  • loj#6030. 「雅礼集训 2017 Day1」矩阵(贪心 构造)

    attack

扫码关注云+社区

领取腾讯云代金券