CF1542B

题面:https://codeforces.com/contest/1542/problem/B

大致题意,一个集合中初始元素为1,规定若x在该集合中,则x*a与x+b也在该集合中,给定n、a、b,判断n是否在该集合中

题解:若n在该集合中,则必定能找到一个数

若两者模数相等且t<=n,则可以通过累加b得到n

若两者模数不同,则只有通过将t*a来改变其模数,若t>n仍未找到模数,则n不在集合内

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<vector>
#include<string>
#include<map>
#include<cstdio>
#include<list>
#include<stack>
#include<unordered_set>
using namespace std;
using ll = int64_t;
ll T, n, m, k;
ll tmp, a, b;
int main()
{
T = 1;
cin >> T;
while(T--)
{
cin >> n >> a >> b;
if(a==1)
{
if((n-1)%b==0)
{
cout << "YES\n";
}else
{
cout << "NO\n";
}
}else
{
ll t = 1;
bool flag = 0;
while(t<=n&&!flag)
{
if(t%b==n%b)
{
flag = 1;
}else
{
t *= a;
}
}
cout << (flag ? "YES\n" : "NO\n");
}
}
return 0;
}