Educational Codeforces Round 22 B. The Golden Age (暴力)

原题地址:http://codeforces.com/contest/813/problem/B

题意 :就是有一个数叫做不幸运数,满足题目的n=x^{a}+y^{b},现在给你一个区间[l,r],让你找一个在这个区间里面一个最长的区间使得这个区间里面的所有数都不是不幸运数,让你输出最长区间的区间长度.

思路:因为这是指数式增长,所以我们一一枚举所有的情况就行了,只需要注意在判断循环结束的时候别用乘法,用除法就行了,因为乘法会爆unsigned long long

#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 998244353;
ull x, y, l, r;
ull a[maxn], b[maxn];
int top1, top2;
set<ull>s;
int main() {
    cin >> x >> y >> l >> r;
    for (ull i = 1; i <= r; i *= x) {
        a[++top1] = i;
        if(i>r/x) break;//注意判断循环结束必须用除法,用乘法会爆ull
    }
    for (ull i = 1; i <= r; i *= y) {
        b[++top2] = i;
        if(i>r/y) break;
    }
    s.insert(l - 1);
    s.insert(r + 1);
    for (int i = 1; i <= top1; i++) {
        for (int j = 1; j <= top2; j++) {
            if (a[i] + b[j] >= l && a[i] + b[j] <= r)s.insert(a[i] + b[j]);
        }
    }
    ull MAX = 0;
    set<ull>::iterator it;
    for (it = s.begin(); it != s.end(); it++) {
        if (it == s.begin()) {
            continue;
        } else {
            ull x = *it;
            it--;
            ull y = *it;
            if (MAX < x - y - 1) {
                MAX = x - y - 1;
            }
            it++;
        }
    }
    cout << MAX << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/81666015