Codeforces 813 B The Golden Age

本文解析了CodeForces上的一道题目,该题要求计算特定范围内幸运数的最大连续区间。通过枚举所有可能的不幸数,并使用排序来确定最大区间的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址: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 0x3f3f3f3f
using 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值