题意

题目链接

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了。

有些细节需要特判一下

  1. #include<bits/stdc++.h>
  2. #define Pair pair<int, int>
  3. #define MP(x, y) make_pair(x, y)
  4. #define fi first
  5. #define se second
  6. #define int long long
  7. #define LL long long
  8. #define ull unsigned long long
  9. #define Fin(x) {freopen(#x".in","r",stdin);}
  10. #define Fout(x) {freopen(#x".out","w",stdout);}
  11. using namespace std;
  12. const int MAXN = 1e6 + 10, INF = 1e9 + 10;;
  13. int mod;
  14. const double eps = 1e-9;
  15. template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
  16. template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
  17. 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;}
  18. 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);}
  19. template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
  20. template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
  21. template <typename A> inline void debug(A a){cout << a << '\n';}
  22. template <typename A> inline LL sqr(A x){return 1ll * x * x;}
  23. 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;}
  24. template <typename A> A inv(A x) {return fp(x, mod - 2);}
  25. inline int read() {
  26. char c = getchar(); int x = 0, f = 1;
  27. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  28. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  29. return x * f;
  30. }
  31. int a, b, X1, End;
  32. //x_{i+1} = (aX_i + b) % P
  33. //a^ans = x % p
  34. //a^{i * k - j} = x % p
  35. //a^{i * k} = x * a^j % p
  36. map<int, int> mp;
  37. /*
  38. int Query(int a, int x, int p) {
  39. if(__gcd(a, p) != 1) return -2;
  40. int base = 1;
  41. for(int i = 0; i <= p; i++) {
  42. if(base % p == x) return i;
  43. mul2(base, a);
  44. }
  45. return -2;
  46. }
  47. */
  48. int Query(int a, int x, int p) {
  49. if(__gcd(a, p) != 1) return -2;
  50. mp.clear(); int block = ceil(sqrt(p)), base = fp(a, block);
  51. for(int i = 0, cur = x; i <= block; i++, mul2(cur, a)) mp[cur] = i;
  52. for(int i = 1, cur = base; i <= block; i++, mul2(cur, base))
  53. if(mp[cur])
  54. return i * block - mp[cur];
  55. return -2;
  56. }
  57. void solve() {
  58. mod = read(); a = read(); b = read(); X1 = read(); End = read();
  59. if(X1 == End) {puts("1"); return ;}
  60. if(!a) {
  61. if(!b) {puts(End == X1 ? "1" : "-1");return ;}
  62. else {puts(End == b ? "2" : "-1");return ;}
  63. }
  64. if(a == 1) {
  65. if(!b) {puts(End == X1 ? "1" : "-1");return ;}
  66. else {
  67. //int tmp = add(End, -X1 + mod) % b;
  68. //cout << tmp << '\n';
  69. cout << mul(add(End, -X1), inv(b)) + 1 << '\n';
  70. return ;
  71. }
  72. }
  73. int tmp = mul(b, inv(a - 1));
  74. add2(X1, tmp); add2(End, tmp);
  75. mul2(End, inv(X1));
  76. cout << Query(a, End, mod) + 1 << '\n';
  77. }
  78. signed main() {
  79. //freopen("a.in", "r", stdin);
  80. for(int T = read(); T--; solve());
  81. return 0;
  82. }

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. js中的观察者模式

    什么事观察者模式: 这是一种创建松散耦合代码的技术.它定义对象间 一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知.由主体和观察者组成,主体负责发布事件,同时观察者通过 ...

  2. Three.js学习笔记04--纹理

    1 纹理由图片组成  3D世界的纹理由图片组成. 将纹理以一定的规则映射到几何体上,一般是三角形上,那么这个几何体就有纹理皮肤了. 首先应该有一个纹理类,其次是有一个加载图片的方法,将这张图片和这个纹 ...

  3. Vue 学习笔记 -- inline-template

    简书 更方便的使用私有子组件 定义一个私有子组件时,如果子组件的template过长会使得代码非常难以阅读 这时可以使用内联模版 但是如果写成这样 为毛用要子组件呢?

  4. 吴恩达机器学习笔记54-开发与评价一个异常检测系统及其与监督学习的对比(Developing and Evaluating an Anomaly Detection System and the Comparison to Supervised Learning)

    一.开发与评价一个异常检测系统 异常检测算法是一个非监督学习算法,意味着我们无法根据结果变量

  5. [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  6. [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  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]LeetCode461. 汉明距离 | Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  9. C# 当中 foreach 的原理

    在 C# 当中的 foreach 语句实际上就是遍历迭代器的语法糖.例如我们拥有以下代码: public class TestClass { public void TestMethod() { va ...

  10. Python内置函数(58)——slice

    英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...