codeforces round 22-B.the golden age
来源:互联网 发布:有哪些搜索算法 编辑:程序博客网 时间:2024/06/05 17:33
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 + 31, 17 = 23 + 32 = 24 + 30) and year 18 isn't unluckyas 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.
The first line contains four integer numbers x, y, l and r (2 ≤ x, y ≤ 1018, 1 ≤ l ≤ r ≤ 1018).
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.
2 3 1 10
1
3 5 10 22
8
2 3 3 5
0
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].
分别对r以内x的a次方和y的b次方进行枚举,构造两个数组,100即可满足。然后枚举两个数组的和,统计在[l,r]范围内的数,在求最大值。
#include<cstdio>#include<iostream>#include<algorithm>#define maxn 10010#define maxm 100000#define ll long longusing namespace std;ll a[maxn];ll b[maxn];ll c[maxm];int main() {int la, lb;ll x, y, l, r;cin >> x >> y >> l >> r;ll t = r;la = lb = 1;a[0] = b[0] = 1;while (1) {ll temp = t / x;if (temp >0) {a[la] = a[la-1]*x;t = t / x;la++;}else break;}t = r;while (1) {ll temp = t / y;if (temp > 0) {b[lb] = b[lb-1]*y;t = t / y;lb++;}else break;}int k = 0;for (int i = 0;i < la;i++) {for (int j = 0;j < lb;j++) {if ((a[i] + b[j]) >= l&&(a[i] + b[j]) <= r) {c[k] = a[i] + b[j];k++;}}}ll ans=0;sort(c, c + k);if (k>0){if (c[0] - l>ans)ans = c[0] - l;if (r - c[k - 1]>ans)ans = r - c[k - 1];for (int i = 1;i < k;i++)ans = max(ans, c[i] - c[i - 1] - 1);}elseans = r - l + 1;cout << ans << endl;return 0;}
- codeforces round 22-B.the golden age
- Educational Codeforces Round 22 B. The Golden Age(枚举)
- codeforces round 22 B.The Golden Age(枚举)
- Educational Codeforces Round 22-B. The Golden Age-暴力枚举+map
- CodeForces 813B The Golden Age
- Codeforces 813 B The Golden Age
- Codeforces 813B The Golden Age 题解
- CodeForces 813B The Golden Age
- The Golden Age CodeForces
- B. The Golden Age
- B.The Golden Age
- B. The Golden Age——math
- The golden age of the developer
- The golden age of the developer
- CF813B:The Golden Age(数学)
- codeforces 2B The least round way
- codeforces 2B The least round way
- codeforces 2B The least round way
- Flooded!
- 【python学习笔记】14:开发一个简易的爬虫
- Android常用控件七之ProgressBar的代码用法
- Android 监听屏幕锁屏(听音频,看视频)
- Word_Search
- codeforces round 22-B.the golden age
- es5.X插件headmaster的简单安装方法
- Android 6.0 运行时权限 easypermissions,permissionsdispatcher ,rxpermissions 三种不同方案的实现
- 线程池
- AngularJS之内置指令汇总
- Java中关于final修饰符的几个知识点
- Ubuntu系统安装QT
- 一种基于C++的扑克牌实现
- 新闻客户端