https://codeforces.com/contest/1542/problem/B
Code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t, n, a, b;
int main()
{
cin >> t;
while(t --)
{
cin >> n >> a >> b;
bool flag = false;
if(!((n - 1) % b)) flag = true;
if(a > 1)
for(ll u = 1; !flag && u <= n; u *= a)
if((n - u) % b == 0) flag = true;
if(flag) cout << "Yes" << endl;
else cout << "No" << endl;
}
}