思路:an=a(n−1)∗X+Y且Y能被X-1整除,设b=y/(x-1),可以推出an=x^n*a0+b*(x^n-1)
两边对a0取模,得an%a0=(x^n-1)*b%a0=0时符合题意
可以看出,若把b消掉,便是欧拉函数了,所以令k=gcd(b,a0)
(x^n-1)*(b/k)%(a0/k)=0,b/k与a0/k互质,所以(b/k)%(a0/k)!=0
得(x^n-1)%(a0/k)=0——>x^n=1(mod(a0/k))
通过欧拉定理求得n的一个解phi(a0/k),题目中找最小的,那就是他的最小符合条件的因子q,满足x^q%(a0/k)=1
#include
#include
using namespace std;
typedef long long ll;
ll x, y, a, t;
ll qpow(ll a, ll b, ll p) {
ll ans = 1;
while(b) {
if(b & 1) {
ans = (ans * a) % p;
}
a = (a * a) % p;
b >>= 1;
}
return ans;
}
ll euler(ll n) {
ll ret = n;
for(ll i = 2; i * i <= n; i++) {
if(n % i == 0) {
ret -= ret / i;
while(n % i == 0)
n /= i;
}
}
if(n > 1)
ret -= ret / n;
return ret;
}
int main() {
cin >> t;
while(t--) {
cin >> x >> y >> a;
if(y == 0) {
cout << 1 << endl;
continue;
}
ll b=y/(x-1);
ll k=__gcd(b,a);
ll ss=a/k;
if(__gcd(x, ss) != 1) {
cout << "Impossible!" << endl;
} else {
ll gg = euler(ss);
ll ans = 1000000000000000000000;
for(ll i = 1; i * i <= gg; i++) {
if(gg % i)
continue;
if(qpow(x, i, ss) == 1) {
ans = min(ans, i);
}
if(qpow(x, gg / i, ss) == 1) {
ans = min(ans, gg / i);
}
}
cout << ans << endl;
}
}
return 0;
}