1542B - Plus and Multiply - CodeForces Solution


constructive algorithms math number theory *1500

Please click on ads to support us..

Python Code:

t=(int)(input())
for i in range(t):
    w=input().split()
    n=(int)(w[0])
    a=(int)(w[1])
    b=(int)(w[2])
    if a==1 :
    	if (n-1)%b==0 :
    		print("Yes")
    	else :
    		print("No")
    	
    else :
    	t=1
    	flag=0
    	while t<=n :
    		if t%b==n%b:
    			flag=1
    			break
    		t=t*a
    	if flag==1:
    		print("Yes")
    	else :
    		print("No")

C++ Code:

#include <bits/stdc++.h>

#define ll long long
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define ve vector<ll>
#define f(i, x, n) for (ll i = x; i < n; i++)
#define f1(i, x, n) for (ll i = x; i <= n; i++)
#define fm(i, n) for (ll i = n; i > 0; i--)
#define fm1(i, n) for (ll i = n - 1; i >= 0; i--)
#define pi pair<ll, ll>
#define fast ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define el <<"\n"
#define rall(arr) (arr).rbegin(), (arr).rend()

using namespace std;

int xd[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int yd[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int N = 1e9 + 7;



void run()
{
    ll n, a, b, x = 1;cin >> n >> a >> b;
    if (a == 1)
    {
        cout << ((n - 1) % b ? "No" : "Yes") el;
        return;
    }
    while (x <= n)
    {
        if ((n-x)%b==0)
        {
            cout << "Yes" el;
            return;
        }
        x *= a;
    }
    cout << "No" el;
}

int main()
{
    setprecision(10);
    fast;
#ifndef ONLINE_JUDGE
    freopen(R"(D:\proplem\in.txt.txt)", "r", stdin);
#endif
   
    int t = 1;cin >> t;
    while (t--)
        run();
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it