#include<bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define mp make_pair
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> Vll;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;
const ll mod = 1e9 + 7;
//const ll mod = 998244353;
const double pi = acos(-1.0);
inline ll ksc(ll x,ll y,ll mod)
{
ll ans = 0;
while (y) {
if (y & 1)
ans = (ans + x) %mod;
y >>= 1;
x = (x + x) %mod;
}
return ans;
}
inline ll qmi (ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
inline int read () {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f?-x:x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x/10);
putchar(x % 10 + '0');
}
inline ll sub (ll a, ll b) {
return ((a - b ) %mod + mod) %mod;
}
inline ll add (ll a, ll b) {
return (a + b) %mod;
}
#define int long long
vi ans;
int n;
int a,b;
map<int, int> st;
bool dfs (int u) {
if (u >= n) {
if (u == n) return 1;
return 0;
}
}
void solve() {
cin >> n >> a >> b;
if (a == 1) {
if (n == 1) {
puts("Yes");
}
else if ((n - 1) % b == 0)
puts("Yes");
else puts("No");
}
else {
int t = 1;
while (t <= n) {
if (t % b == n %b) {
puts("YES");
return;
}
t = t *a;
}
puts("NO");
}
}
signed main () {
int t;
cin >> t;
while (t --) solve();
return 0;
}
首先从特殊的式子开始考虑,然后猜想一般的式子。
$$ x[1] = (x + b) * a = a *x + a*b\\ x[2] = a^2*x + a^2*b\\ x[3] = a^2*x + a^2*b+b = a^2*x + b*(a^2+1)\\ 由此我们可以猜想一个统一的表达式\\ x[n] = a^{c_1}*x + c_2*b\\ 观察这个式子,我们可以发现\\ ,的时候,只有*a才会产生新的余数,这个时候,我们可以发现通过*a,才会产生新的余数,\\所以只要当存在这个m的时候,才是YES,否则NO $$