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

I want to win Miss Universe Nigeria like my mum — Dr Danielle Adewusi

Medical doctor and beauty pageant contestant Dr. Danielle Adewusi is making headlines across the nation as she embarks on an inspiring quest to win...

The Real Truth About Led Strip Lights Room Decor Before You Buy

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

Made in Abyss: Mezameru Shinpi Anime Film Unveils Trailer, Cast, Theme Song

Anime fans around the globe have reason to celebrate as the highly anticipated film Made in Abyss: Mezameru Shinpi has officially revealed its stunning...

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!