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

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

#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;
}

📦 Looking for Codeforces Round 407 Div Problem Masha? Check the best deals on Amazon.

🛒 Shop Codeforces Round 407 Div Problem Masha on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

#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;
}

📦 Looking for Codeforces Round 407 Div Problem Masha? Check the best deals on Amazon.

🛒 Shop Codeforces Round 407 Div Problem Masha on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

The Real Truth About Viral Kitchen Gadgets Before You Buy

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Tiger Woods pleads no contest to reckless driving charge in Florida

Golf legend Tiger Woods has entered a formal plea in a Florida courtroom, bringing an end to months of legal uncertainty following a serious...

We Tested Viral Kitchen Gadgets Here Is What You Must Know

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

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!