其实如果 n 在 S 中的话,那么 n 一定可以被写成:
n
=
a
x
+
b
∗
y
n = a^x + b*y
n=ax+b∗y, (其中 x,y 是恰当的数字),这个可以自己在纸上随便构造 S 中的元素,一定可以通过表达式化简写成:a^x + b*y 这样的形式,
如果我们枚举 a^x 话只需要判读
(
n
−
a
x
)
(n-a^x) % b
(n−ax) 是不是等于 0 就可以了,如果是就是 yes,否则 no
真的是有些题,知识托着脑袋想是想不出来的,还是要在纸上多写写画画,才能出答案!!!
代码
#include<bits/stdc++.h>
using namespace std;#definedbdouble#definelllonglong#definePirpair<int,int>#definefifirst#definesesecond#definepbpush_back#definem_pmake_pair#defineinf0x3f3f3f3f#defineINF0x3f3f3f3f3f3f3f3f/*==========ACMer===========*/#defineintllconstint N =2e5+10;int a[N];signedmain(){int T;scanf("%lld",&T);while(T --){int n, a, b;scanf("%lld %lld %lld",&n,&a,&b);if(b ==1){
cout <<"YES\n";continue;}if(a ==1){if((n -1)% b ==0)
cout <<"YES\n";else
cout <<"NO\n";continue;}int flag =0;for(ll i =1; i <= n; i *= a){if((n - i)% b ==0){
flag =1;}}if(flag)
cout <<"YES\n";else
cout <<"NO\n";}return0;}