CF1542B Plus and Multiply

CF1542B Plus and Multiply

强者黄题,被打败了……

注意到合法的答案可以写成 \(a ^ x + yb\)

证明:令 \(t = a ^ x + yb\)\(a \times t = a ^ {x + 1} + (ay)b\),仍然满足上述形式。\(t + b = a ^ {x} + (y + 1)b\),也满足。

问题转化为判断是否满足 \(n \equiv a ^ x \pmod{b}\)。枚举 \(x\),暴力做即可。

细节: \(a ^ x \le n\),循环里面条件要写好,注意不能少算或多算一次。

#include<bits/stdc++.h>
#define F(i,l,r) for(int i(l); i <= (r); ++ i)
#define G(i,r,l) for(int i(r); i >= (l); -- i)
#define int ll
using namespace std;
using ll = long long;
const int N = 2e5;
int n, a, b;
void Main(){ 
	cin >> n >> a >> b;
	if(n == 1){
		cout << "Yes\n";
	}
	else if(b == 1){
		cout << "Yes\n";
	}
	else if(a == 1){
		if(n % b == 1){
			cout << "Yes\n";
		}
		else{
			cout << "No\n";
		}
	}
	else{
		bool flag = 0;
		for(int tmp = 1; tmp <= n; tmp = tmp * a){
			if(tmp % b == n % b){
				flag = 1;
			}
		}
		if(flag) cout << "Yes\n";
		else cout << "No\n";
	} 
	return ;
}
signed main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int T;
	cin >> T; 
	while(T --) Main(); 
	return fflush(0), 0;
}
posted @ 2026-09-02 14:50  superl61  阅读(4)  评论(0)    收藏  举报