BZOJ3122: [Sdoi2013]随机数生成器(BSGS)
程序员文章站
2022-04-08 19:13:42
题意 "题目链接" 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 $$ 然后再合并一下就可以 ......
题意
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; }
上一篇: QQ群推广技巧之双簧记
下一篇: git方式安装thinkphp5