题意

题目链接

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

BZOJ3122: [Sdoi2013]随机数生成器(BSGS)的更多相关文章

  1. [bzoj3122][SDOI2013]随机数生成器 ——BSGS,数列

    题目大意 给定递推序列: F[i] = a*F[i-1] + b (mod c) 求一个最小的i使得F[i] == t 题解 我们首先要化简这个数列,作为一个学渣,我查阅了一些资料: http://d ...

  2. bzoj3122 [SDOI2013]随机数生成器

    bzoj3122 [SDOI2013]随机数生成器 给定一个递推式, \(X_i=(aX_{i-1}+b)\mod P\) 求满足 \(X_k=t\) 的最小整数解,无解输出 \(-1\) \(0\l ...

  3. 【BZOJ3122】[Sdoi2013]随机数生成器 BSGS+exgcd+特判

    [BZOJ3122][Sdoi2013]随机数生成器 Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数.   接下来T行,每行有五个整数p,a,b, ...

  4. 【BZOJ-3122】随机数生成器 BSGS

    3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1362  Solved: 531[Submit][Sta ...

  5. 【BZOJ 3122】 [Sdoi2013]随机数生成器 (BSGS)

    3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1442  Solved: 552 Description ...

  6. 【bzoj3122】[Sdoi2013]随机数生成器 BSGS思想的利用

    题目描述 给出递推公式 $x_{i+1}=(ax_i+b)\mod p$ 中的 $p$.$a$.$b$.$x_1$ ,其中 $p$ 是质数.输入 $t$ ,求最小的 $n$ ,使得 $x_n=t$ . ...

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

    题目 输入格式 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. 注意:P一定为质数 输出 ...

  8. bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器

    http://www.lydsy.com/JudgeOnline/problem.php?id=3122 等比数列求和公式+BSGS #include<map> #include<c ...

  9. bzoj 3122 : [Sdoi2013]随机数生成器 BSGS

    BSGS算法 转自:http://blog.csdn.net/clove_unique 问题 给定a,b,p,求最小的非负整数x,满足$a^x≡b(mod \ p)$ 题解 这就是经典的BSGS算法, ...

随机推荐

  1. C# 开源组件--NPOI读取Excel单元格中的公式值

    今天在项目中碰到了EXCEL导入的数据是用公式生成,直接导入不了数据,写在博客中方便自已查询也可以给想找这方面的参考一下: 用NPOI导入时,在OFFICE 2007中的文件导入时一般会用XSSF,所 ...

  2. Xtrabackup实现Mysql的InnoDB引擎热备份

    前面Zabbix使用的数据库是mysql,数据库备份不用多说,必须滴,由于使用的是innodb引擎,既然做,那就使用第三方强大的Xtrabackup工具来热备吧,Xtrabackup的说明,参见htt ...

  3. 分门别类总结Java中的各种锁,让你彻底记住

    概念 公平锁/非公平锁 公平锁是指多个线程按照申请锁的顺序来获取锁. 非公平锁是指多个线程获取锁的顺序并不是按照申请锁的顺序,有可能后申请的线程比先申请的线程优先获取锁.有可能,会造成优先级反转或者饥 ...

  4. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  5. [Swift]LeetCode62. 不同路径 | Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  6. [Swift]LeetCode295. 数据流的中位数 | Find Median from Data Stream

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  7. [Swift]LeetCode325. 最大子数组之和为k $ Maximum Size Subarray Sum Equals k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. [Swift]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

    Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...

  9. jupyter-notebook后home页面空白问题

    jupyter-notebook后home页面空白问题 解决方案1   更换默认的浏览器,选择谷歌浏览器,很多360打不开的页面,更换谷歌后都能有效解决,并且确保是最新版本的google浏览器. 解决 ...

  10. .NET Core实战项目之CMS 第十七章 CMS网站系统的部署

    目前我们的.NET Core实战项目之CMS系列教程基本走到尾声了,通过这一系列的学习你应该能够轻松应对.NET Core的日常开发了!当然这个CMS系统的一些逻辑处理还需要优化,如没有引入日志组件以 ...