Codeforces Round #729 (Div. 2) 1542B - Plus and Multiply Solution
Problem Link: https://codeforces.com/problemset/problem/1542/B
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n, a, b;
- cin >> n >> a >> b;
- if (n == 1 || b == 1 || n % b == 1) {
- cout << "Yes" << endl;
- continue;
- } else if (a == 1) {
- if (n % b == 1)
- cout << "Yes" << endl;
- else
- cout << "No" << endl;
- } else {
- bool check = false;
- long long s;
- int i = 0;
- while (pow(a, i) <= n) {
- s = n - pow(a, i);
- if (s % b == 0) {
- check = true;
- break;
- }
- i++;
- }
- if (check)
- cout << "Yes" << endl;
- else
- cout << "No" << endl;
- }
- }
- return 0;
- }
No comments