题意
题目链接
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; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/604618.html