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.
想出来的公式:
x和y代表个数
只要判断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