您的位置:首页 > 编程语言 > Go语言

Codeforces 813 B The Golden Age

2017-06-06 17:13 375 查看
题目地址: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
, b
;
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 << end
becb
l;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: