Codeforces Round #729 (Div. 2) 1542B - Plus and Multiply Solution


 

 Problem Link: https://codeforces.com/problemset/problem/1542/B

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. int32_t main() {
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0);
  12. int t;
  13. cin >> t;
  14. while (t--) {
  15. int n, a, b;
  16. cin >> n >> a >> b;
  17. if (n == 1 || b == 1 || n % b == 1) {
  18. cout << "Yes" << endl;
  19. continue;
  20. } else if (a == 1) {
  21. if (n % b == 1)
  22. cout << "Yes" << endl;
  23. else
  24. cout << "No" << endl;
  25. } else {
  26. bool check = false;
  27. long long s;
  28. int i = 0;
  29. while (pow(a, i) <= n) {
  30. s = n - pow(a, i);
  31. if (s % b == 0) {
  32. check = true;
  33. break;
  34. }
  35. i++;
  36. }
  37. if (check)
  38. cout << "Yes" << endl;
  39. else
  40. cout << "No" << endl;
  41. }
  42. }
  43. return 0;
  44. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.