Codeforces 813 B The Golden Age

来源:互联网 发布:南风知我意结局是什么 编辑:程序博客网 时间:2024/06/05 04:44

题目地址:http://codeforces.com/contest/813/problem/B
题意:就是告诉你x,y两个值,让你通过 n = x^a + y^b,求出的n就是不幸运数,让你求出l~r范围内幸运数连续的最长区间是多少。
思路:就是把在l~r范围内的全部的不幸运数求出来,记录下来。求最长的区间长度就是把全部不幸运数排序,再现在在这个数组里相邻的两个不幸运数相减再减一就是区间长度,但是l和r这两个区间端点需要特判,就是max(v[0] - l, r - v[v.size() - 1])。(详细请看代码)

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <algorithm>#define N 110#define LL long long #define inf 0x3f3f3f3fusing namespace std;LL a[N], b[N];vector<LL> v;int main() {    cin.sync_with_stdio(false);    LL x, y, l, r;    LL lena, lenb;    while (cin >> x >> y >> l >> r) {        a[0] = 1;        b[0] = 1;        lena = 1;        lenb = 1;        while (r/x>=a[lena-1]){            a[lena] = a[lena - 1] * x;            lena++;        }        while (r / y >= b[lenb - 1]) {            b[lenb] = b[lenb - 1] * y;            lenb++;        }        v.clear();        for (int i = 0; i < lena; i++) {            for (int j = 0; j < lenb; j++) {                if (a[i] + b[j] >= l) {                    if (a[i] + b[j] > r) {                        break;                    }                    v.push_back(a[i] + b[j]);                }            }        }        sort(v.begin(), v.end());        LL ans = 0;        if (!v.size()) {            ans = r - l + 1;        }        else {            ans = max(v[0] - l, r - v[v.size() - 1]);        }        for (int i = 0; i < v.size(); i++) {            ans = max(ans, v[i] - l - 1);            l = v[i];        }        cout << ans << endl;    }    return 0;}
原创粉丝点击