BZOJ3122: [Sdoi2013]随机数生成器(BSGS)
题意
Sol
这题也比较休闲。
直接把\(X_{i+1} = (aX_i + b) \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)的更多相关文章
- [bzoj3122][SDOI2013]随机数生成器 ——BSGS,数列
题目大意 给定递推序列: F[i] = a*F[i-1] + b (mod c) 求一个最小的i使得F[i] == t 题解 我们首先要化简这个数列,作为一个学渣,我查阅了一些资料: http://d ...
- bzoj3122 [SDOI2013]随机数生成器
bzoj3122 [SDOI2013]随机数生成器 给定一个递推式, \(X_i=(aX_{i-1}+b)\mod P\) 求满足 \(X_k=t\) 的最小整数解,无解输出 \(-1\) \(0\l ...
- 【BZOJ3122】[Sdoi2013]随机数生成器 BSGS+exgcd+特判
[BZOJ3122][Sdoi2013]随机数生成器 Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b, ...
- 【BZOJ-3122】随机数生成器 BSGS
3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1362 Solved: 531[Submit][Sta ...
- 【BZOJ 3122】 [Sdoi2013]随机数生成器 (BSGS)
3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1442 Solved: 552 Description ...
- 【bzoj3122】[Sdoi2013]随机数生成器 BSGS思想的利用
题目描述 给出递推公式 $x_{i+1}=(ax_i+b)\mod p$ 中的 $p$.$a$.$b$.$x_1$ ,其中 $p$ 是质数.输入 $t$ ,求最小的 $n$ ,使得 $x_n=t$ . ...
- BZOJ3122 [Sdoi2013]随机数生成器 【BSGS】
题目 输入格式 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. 注意:P一定为质数 输出 ...
- bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器
http://www.lydsy.com/JudgeOnline/problem.php?id=3122 等比数列求和公式+BSGS #include<map> #include<c ...
- bzoj 3122 : [Sdoi2013]随机数生成器 BSGS
BSGS算法 转自:http://blog.csdn.net/clove_unique 问题 给定a,b,p,求最小的非负整数x,满足$a^x≡b(mod \ p)$ 题解 这就是经典的BSGS算法, ...
随机推荐
- js中的观察者模式
什么事观察者模式: 这是一种创建松散耦合代码的技术.它定义对象间 一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知.由主体和观察者组成,主体负责发布事件,同时观察者通过 ...
- Three.js学习笔记04--纹理
1 纹理由图片组成 3D世界的纹理由图片组成. 将纹理以一定的规则映射到几何体上,一般是三角形上,那么这个几何体就有纹理皮肤了. 首先应该有一个纹理类,其次是有一个加载图片的方法,将这张图片和这个纹 ...
- Vue 学习笔记 -- inline-template
简书 更方便的使用私有子组件 定义一个私有子组件时,如果子组件的template过长会使得代码非常难以阅读 这时可以使用内联模版 但是如果写成这样 为毛用要子组件呢?
- 吴恩达机器学习笔记54-开发与评价一个异常检测系统及其与监督学习的对比(Developing and Evaluating an Anomaly Detection System and the Comparison to Supervised Learning)
一.开发与评价一个异常检测系统 异常检测算法是一个非监督学习算法,意味着我们无法根据结果变量
- [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [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 ...
- [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 ...
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- C# 当中 foreach 的原理
在 C# 当中的 foreach 语句实际上就是遍历迭代器的语法糖.例如我们拥有以下代码: public class TestClass { public void TestMethod() { va ...
- Python内置函数(58)——slice
英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...