#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #define LL long long #define ll long long usingnamespacestd; LL A, B, C; structhash { staticconst LL Mod = 100007; LL a[Mod + 100], v[Mod + 100]; LL Find(LL x) { int y = x % Mod; while (a[y] != x && a[y] != -1) (y += 1) %= Mod; return y; } voidInsert(LL x, LL y) { LL z = Find(x); if (a[z] == -1) { a[z] = x; v[z] = y; } } LL Get(LL x) { LL y = Find(x); return a[y] == x ? v[y] : -1; } voidClear() { memset(a, 0xff, sizeof(a)); } } S; inline LL Pow(LL a, LL b) { LL Ans = 1; while (b) { if (b & 1) (Ans *= a) %= C; (a *= a) %= C; b >>= 1; } return Ans; } inline LL Ex_gcd(LL a, LL b, LL &x, LL &y) { if (!b) { x = 1; y = 0; return a; } LL Gcd = Ex_gcd(b, a % b, x, y); LL Tmp = x; x = y; y = (Tmp - (a / b) * y); return Gcd; }
inlineintBSGS() { S.Clear(); LL M = ceil(sqrt((double)C)); LL Base = 1; for (LL i = 0; i < M; ++i) { S.Insert(Base, i); Base = Base * A % C; } LL D = 1; for (LL i = 0; i < M; ++i) { LL x, y; Ex_gcd(D, C, x, y); x = ((x * B) % C + C) % C; LL k = S.Get(x); if (k != -1) return i * M + k; (D *= Base) %= C; } return-1; } intmain() { /*#ifndef ONLINE_JDUGE freopen("A.in", "r", stdin); freopen("A.out", "w", stdout); #endif */ while (scanf("%lld%lld%lld", &C, &A, &B)!= EOF) { // cout<<C<<" "<<A<<" "<<B<<endl; LL Ans = BSGS(); if (Ans == -1) puts("no solution"); elseprintf("%lld\n", Ans); } return0; }