构造 蓝桥OJ小蓝的无限集

构造 蓝桥OJ小蓝的无限集_第1张图片

样例输入 

4

1 4 7

2 5 8

3 6 8

12 11 81

样例输出 

No

Yes

No

No

构造 蓝桥OJ小蓝的无限集_第2张图片 

#include
using namespace std;

using ll = long long;

bool rnk(ll a, ll b, ll n)
{
  if((n-1) % b == 0) return true;
  else if (a == 1) return false;
  ll res = 1;
  while(res < n)
  {
    res *= a;
    if (res > n) break;
    else if (res == n) return true;
    else if ((n-res) % b == 0) return true;
  }
  return false;
}

int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int t; cin >> t;
  while(t--)
  {
    ll a, b, n;
    cin >> a >> b >> n;
    string ans = rnk(a,b,n)?"Yes":"No";
    cout << ans <<'\n';
  }
  return 0;
}

 

 

你可能感兴趣的:(蓝桥杯备赛练习,算法,c++)