• BZOJ3122 [Sdoi2013]随机数生成器 【BSGS】


    题目

    输入格式

    输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数。

    接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据。保证X1和t都是合法的页码。

    注意:P一定为质数

    输出格式

    共T行,每行一个整数表示他最早读到第t页是哪一天。如果他永远不会读到第t页,输出-1。

    输入样例

    3

    7 1 1 3 3

    7 2 2 2 0

    7 2 2 2 1

    输出样例

    1

    3

    -1

    提示

    0<=a<=P-1,0<=b<=P-1,2<=P<=10^9

    题解

    运用数列的知识可以将式子化简为一个等比数列
    然后就可以用BSGS求解

    但是要分很多特殊情况讨论 = =
    例如(a = 0,a = 1,X1 = t)之类的

    还有,,
    BSGS时,开根要向上取整,保证查找真的完全了

    数学真差

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #include<cstring>
    #include<algorithm>
    #define LL long long int
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
    using namespace std;
    const int maxn = 100005,maxm = 100005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    LL P;
    map<LL,LL> mp;
    LL qpow(LL a,LL b){
    	LL ans = 1;
    	for (; b; b >>= 1,a = a * a % P)
    		if (b & 1) ans = ans * a % P;
    	return ans % P;
    }
    LL inv(LL a){
    	return qpow(a,P - 2);
    }
    void solve1(LL A,LL B,LL X,LL T){
    	LL ans = ((T - X) % P * inv(B) % P + P) % P;
    	printf("%lld
    ",ans + 1);
    }
    LL BSGS(LL a,LL b){
    	mp.clear();
    	if (a % P == 0) return -2;
    	LL m = (LL)ceil(sqrt(P)),ans;
    	for (int i = 0; i <= m; i++){
    		if (i == 0){
    			ans = b % P;
    			mp[ans] = i;
    		}
    		else {
    			ans = ans * a % P;
    			mp[ans] = i;
    		}
    	}
    	LL t = qpow(a,m); ans = t;
    	for (int i = 1; i <= m; i++){
    		if (i != 1) ans = ans * t % P;
    		if (mp.count(ans)){
    			ans = ((i * m - mp[ans]) % P + P) % P;
    			return ans;
    		}
    	}
    	return -2;
    }
    void solve2(LL A,LL B,LL X,LL T){
    	LL tmp = B * inv(A - 1) % P;
    	LL a = A,b = (T + tmp) % P * inv(X + tmp) % P;
    	printf("%lld
    ",BSGS(a,b) + 1);
    }
    int main(){
    	int T = read(),a,b,X1,t;
    	while (T--){
    		P = read(),a = read(),b = read(),X1 = read(),t = read();
    		if (X1 == t) puts("1");
    		else if (a == 0){
    			if (t == b) puts("2");
    			else puts("-1");
    		}
    		else if (a == 1){
    			if (b == 0) puts("-1");
    			else solve1(a,b,X1,t);
    		}
    		else solve2(a,b,X1,t);
    	}
    	
    	return 0;
    }
    
    
  • 相关阅读:
    Linux之基础系统优化
    Linux之shell命令
    Django解决跨域问题
    Django中使用geetest验证
    python2与python3的区别
    一个长得很丑的登录和注册
    Django组件-forms组件
    Django组件-中间件
    cookie、session与用户认证组件
    jquery练习
  • 原文地址:https://www.cnblogs.com/Mychael/p/8601761.html
Copyright © 2020-2023  润新知