1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 200006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int P; int n , m;
int Pow( int x , int a ) { int ret = 1; while( a ) { if( a & 1 ) ret = 1ll * ret * x % P; x = 1ll * x * x % P , a >>= 1; } return ret; }
int p , a , b , x1 , t; map<int,int> M; void solve() { M.clear(); scanf("%d%d%d%d%d",&P,&a,&b,&x1,&t);
if( b == 0 && a == 1 ) return printf("%d\n",x1 == t ? 1 : -1) , void(); if( a == 1 ) { t = ( t + P - x1 ) % P; printf("%d\n",t * 1ll * Pow( b , P - 2 ) % P + 1); return; } if( a == 0 ) { printf("%d\n",x1 == t ? 1 : t == b ? 2 : -1 ); return; } int lam = 1ll * b * Pow( a - 1 , P - 2 ) % P; t = ( t + lam ) % P , x1 = ( x1 + lam ) % P; if( !t && !x1 ) return puts("1") , void(); t = t * 1ll * Pow( x1 , P - 2 ) % P; if( t == 0 ) return puts("-1") , void(); int r = ceil( sqrt( P ) ) , f = 1; M[f] = 0; for( int i = 1 ; i <= r ; ++ i ) { f = 1ll * f * a % P; if( !M.count( f ) ) M[f] = i; } int bh = Pow( a , ( ( P - 2 ) * 1ll * r ) % ( P - 1 ) ); if( M.count( t ) ) return printf("%d\n",M[t] + 1) , void(); for( int i = 1 ; i <= r ; ++ i ) { t = 1ll * t * bh % P; if( M.count( t ) ) return printf("%d\n",M[t] + 1 + i * r ) , void(); } puts("-1"); }
signed main() {
int T;cin >> T;while( T-- ) solve();
}
|