B. Plus and Multiply

Problem - B - Codeforces

There is an infinite set generated as follows:

  • 11 is in this set.
  • If xx is in this set, x⋅ax⋅a and x+bx+b both are in this set.

For example, when a=3a=3 and b=6b=6, the five smallest elements of the set are:

  • 11,
  • 33 (11 is in this set, so 1⋅a=31⋅a=3 is in this set),
  • 77 (11 is in this set, so 1+b=71+b=7 is in this set),
  • 99 (33 is in this set, so 3⋅a=93⋅a=9 is in this set),
  • 1313 (77 is in this set, so 7+b=137+b=13 is in this set).

Given positive integers aa, bb, nn, determine if nn is in this set.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1051≤t≤105) — the number of test cases. The description of the test cases follows.

The only line describing each test case contains three integers nn, aa, bb (1≤n,a,b≤1091≤n,a,b≤109) separated by a single space.

Output

For each test case, print "Yes" if nn is in this set, and "No" otherwise. You can print each letter in any case.

Example

input

Copy

5
24 3 5
10 3 6
2345 1 4
19260817 394 485
19260817 233 264

output

Copy

Yes
No
Yes
No
Yes

Note

In the first test case, 2424 is generated as follows:

  • 11 is in this set, so 33 and 66 are in this set;
  • 33 is in this set, so 99 and 88 are in this set;
  • 88 is in this set, so 2424 and 1313 are in this set.

Thus we can see 2424 is in this set.

The five smallest elements of the set in the second test case is described in statements. We can see that 1010 isn't among them.

想出来的公式:a^x+b*y=u

x和y代表个数

y=(u-a^x)/b只要判断y是不是整数就行,也就是等式右边可以被整除。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<string.h>
using namespace std;
// ctrl+shift+C 注释
//ctrl+shift+x 取消
#define int long long
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const ll M=1e18+10;
const int mod=1e9+7;
int aa[N],sum[N];
priority_queue<int,vector<int>,greater<int> >pq;
set<int>se;
map<int,int>mp;
queue<int>qu;
vector<int>v;
deque<int>de;
int u,a,b;
void solve()
{
    cin>>u>>a>>b;
    if(a==1)// 特判一下
    {
        if((u-1)%b==0)
        {
            cout<<"Yes"<<endl;
        }
        else cout<<"No"<<endl;
        return;
    }
    int r=log10(u)/log10(a);
   for(int i=0;pow(a,i)<=u;i++)
   {
      int res=u-pow(a,i);
       if(res%b==0)
       {
           cout<<"Yes"<<endl;
           return;
       }
   }
    cout<<"No"<<endl;
}
signed main()
{
   int t=1;
   cin>>t;
   while(t--)
   {
       solve();
   }
}

激动我居然写出来了qaq

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值