codeforces 789 B. Masha and geometric

链接

B. Masha and geometric depression

题意

给你一个等比数列的首项b_1和公比q,然后给出一个上限l,m个数字,在这个等比数列里,小于l且没有在m个数字里面出现过的b_i可以写在黑板上,问最后能写在黑板上的数字有多少个

做法

坑点主要都在b1和q上,我们只需要特判掉q=pm1和q=0或者b_1=0的情况,然后用set存m个数字(可以去重),再暴力到b_i>=l就可以了。

代码

/*set容器中count(x)返回x的数量1或0,se.find(b1) == se.end()判断b1是不是不在se里面,不在的话返回1,在的话返回0*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
int main() {
    LL a[maxn];
    LL b1, q, l, m;
    while(cin >> b1 >> q >> l >> m) {
        LL ans = 0;
        LL cnt = 0;
        set<int> se;
        for(LL i = 0; i < m; i++) {
            cin >> a[i];
            se.insert(a[i]);
        }
        if(abs(b1) > l)
            cout << 0 << endl;
        else if(!b1 || !q) {
            if(!se.count(0))
                cout << "inf" << endl;
            else
                cout << (se.find(b1) == se.end()) << endl;
        } else if(abs(q) == 1) {
            if((!se.count(b1)) || (!se.count(b1 * q)))
                cout << "inf" << endl;
            else
                cout << 0 << endl;
        } else {
            LL sum = b1;
            int cnt = 0;
            while(llabs(sum) <= l) {
                if (se.find(sum) == se.end()) {
                    cnt ++;
                }
                sum *= q;
            }
            cout << cnt << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhien-aa/p/6652029.html