CF - 813B. The Golden Age - 数学+暴力+二分

26 篇文章 0 订阅
26 篇文章 0 订阅

1.题目描述:

B. The Golden Age
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a and b are non-negative integer numbers.

For example, if x = 2 and y = 3 then the years 4 and 17 are unlucky (4 = 20 + 3117 = 23 + 32 = 24 + 30) and year 18 isn't unlucky as there is no such representation for it.

Such interval of years that there are no unlucky years in it is called The Golden Age.

You should write a program which will find maximum length of The Golden Age which starts no earlier than the year l and ends no later than the year r. If all years in the interval [l, r] are unlucky then the answer is 0.

Input

The first line contains four integer numbers xyl and r (2 ≤ x, y ≤ 10181 ≤ l ≤ r ≤ 1018).

Output

Print the maximum length of The Golden Age within the interval [l, r].

If all years in the interval [l, r] are unlucky then print 0.

Examples
input
2 3 1 10
output
1
input
3 5 10 22
output
8
input
2 3 3 5
output
0
Note

In the first example the unlucky years are 2, 3, 4, 5, 7, 9 and 10. So maximum length of The Golden Age is achived in the intervals [1, 1][6, 6] and [8, 8].

In the second example the longest Golden Age is the interval [15, 22].


2.题意概述:

给你一段区间[l,r]和x,y,定义某个数是unlucky当它可以被表示为xa + yb。要你求[l,r]区间内,最大的连续区间使得整个区间的数都不是unlucky的。

3.解题思路:

1e18=260,所以根据数据范围,每个数它的幂次不会超过60,我们完全可以考虑暴力:先预处理出所有小于1e18的次幂。再处理处他们之间的所有组合。

然后再去二分出那些位于l和r区间的次幂组合。因为题目是求连续区间,排序以后再O(N)地更新一遍最大值即可。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100010
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
vector<ll> v1, v2, v;
void init(ll x, ll y)
{
	ll num = 1;
	v1.push_back(num);
	v2.push_back(num);
	ll upx = 1e18 / x, upy = 1e18 / y;
	while (num <= upx)
	{
		num *= x;
		v1.push_back(num);
	}
	num = 1;
	while (num <= upy)
	{
		num *= y;
		v2.push_back(num);
	}
	int sz1 = v1.size(), sz2 = v2.size();
	for (int i = 0; i < sz1; i++)
		for (int j = 0; j < sz2; j++)
				v.push_back(v1[i] + v2[j]);
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	ll x, y, l, r;
	scanf("%I64d%I64d%I64d%I64d", &x, &y, &l, &r);
	init(x, y);
	int pos1 = lower_bound(v.begin(), v.end(), l - 1) - v.begin();
	int pos2 = upper_bound(v.begin(), v.end(), r) - v.begin();
	ll ans = 0;
	if (pos1 == pos2)
		ans = max(ans, r - l + 1);
	else
	{
		ans = max(ans, min(v[pos1], r) - l);
		for (int i = pos1; i < pos2; i++)
			if (i == pos2 - 1)
				ans = max(ans, r - v[i]);
			else
				ans = max(ans, v[i + 1] - v[i] - 1);
	}
	printf("%I64d\n", ans);
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值