Codeforces Round #407 (Div. 2), problem: (B) Masha and geometric depression Solution in C

#include <iostream>
#include <map>
using namespace std;

map<long long, int> was;
map<long long, int> bad;

int main() {
long long b, q, l, m;
cin >> b >> q >> l >> m;
for(int i = 0; i < m; i++) {
long long a;
cin >> a;
bad[a] = 1;
}
if(q == 1 and bad[b] == 1) {
cout << 0 << endl;
return 0;
}
long long cur = b, ans = 0;
bool ok = (bad[0] == 1), ok1 = 0;
while(abs(cur) <= l) {
if(!bad[cur]) ok1 = 1;
if(cur == 0 and ok) break;
if(was[cur] == 1 and bad[cur] == 0) {
cout << “inf” << endl;
return 0;
}
if(was[cur] and !ok1) {
cout << 0 << endl;
return 0;
}
if(bad[cur] == 1) {
was[cur] = -1;
cur *= q;
continue;
}
was[cur] = 1;
ans++;
cur *= q;
}
cout << ans << endl;
return 0;
}

#include <iostream>
#include <map>
using namespace std;

map<long long, int> was;
map<long long, int> bad;

int main() {
long long b, q, l, m;
cin >> b >> q >> l >> m;
for(int i = 0; i < m; i++) {
long long a;
cin >> a;
bad[a] = 1;
}
if(q == 1 and bad[b] == 1) {
cout << 0 << endl;
return 0;
}
long long cur = b, ans = 0;
bool ok = (bad[0] == 1), ok1 = 0;
while(abs(cur) <= l) {
if(!bad[cur]) ok1 = 1;
if(cur == 0 and ok) break;
if(was[cur] == 1 and bad[cur] == 0) {
cout << “inf” << endl;
return 0;
}
if(was[cur] and !ok1) {
cout << 0 << endl;
return 0;
}
if(bad[cur] == 1) {
was[cur] = -1;
cur *= q;
continue;
}
was[cur] = 1;
ans++;
cur *= q;
}
cout << ans << endl;
return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

7 Reasons This Reusable Food Storage Bags Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

LoanDepot Q2 Revenue Jumps 18% on Home Equity Expansion

Mortgage lender LoanDepot has reported a strong financial turnaround for the second quarter, largely fueled by its aggressive and strategic expansion into home equity...

I Tried Phone Stand For Desk For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases. If you had told me six months ago that a single purchase would change how...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!