CF813B:The Golden Age(数学)

本文介绍了一个算法问题,目标是在给定区间内找到最长的连续GoldenAge,即不存在由特定形式表示的不幸年份的时期。通过预先计算并排序可能的不幸年份,使用二分查找等技术高效求解。

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].


题意:找出区间内最长的连续Golden Age。

思路:INT64最多只有二的六十多次方,所以可以将所有数枚举出来,排一排序即可。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[5000], b[70], c[70];
int main()
{
    int cnt=0;
    LL x, y, l, r, ans = 0;
    scanf("%lld%lld%lld%lld",&x,&y,&l,&r);
    for(LL t=1LL; t>0&&t<=r; t*=x) {b[++b[0]] = t; if(r*1.0/t<x) break;}
    for(LL t=1LL; t>0&&t<=r; t*=y) {c[++c[0]] = t; if(r*1.0/t<y) break;}
    for(int i=1; i<=b[0]; ++i)
        for(int j=1; j<=c[0]; ++j)
            a[cnt++] = b[i]+c[j];
    sort(a, a+cnt);
    cnt = unique(a, a+cnt)-a;
    int p = lower_bound(a, a+cnt, l) - a;
    if(p == cnt || a[p] > r) return 0*printf("%lld\n",r-l+1);
    ans = a[p]-l;
    for(++p; p<cnt&&a[p]<=r; ++p)
        ans = max(ans, a[p]-a[p-1]-1);
    ans = max(ans, r-a[p-1]);
    printf("%lld\n",ans);
    return 0;
}


(cobra-env) PS D:\实验室\python> pip install cobra==0.21.0 numpy==1.13 pandas==1.0 Collecting cobra==0.21.0 Using cached cobra-0.21.0-py2.py3-none-any.whl.metadata (9.2 kB) Collecting numpy==1.13 Using cached numpy-1.13.0.zip (5.0 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [28 lines of output] Running from numpy source directory. <string>:367: UserWarning: Unrecognized setuptools command, proceeding with generating Cython sources and expanding templates C:\Users\luwen\AppData\Local\Temp\pip-install-e2roc4mf\numpy_c24b12f813b24ec6 b8031ec1b4ffb42f\numpy\distutils\misc_util.py:469: SyntaxWarning: "is" with a literal. Did you mean "=="? return is_string(s) and ('*' in s or '?' is s) Traceback (most recent call last): File "D:\实验室\python\cobra-env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() File "D:\实验室\python\cobra-env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "D:\实验室\python\cobra-env\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 175, in prepare_metadata_for_build_wheel return hook(metadata_directory, config_settings) File "C:\Users\luwen\AppData\Local\Temp\pip-build-env-tqgiy54n\overlay\Lib\ site-packages\setuptools\build_meta.py", line 374, in prepare_metadata_for_build_wheel self.run_setup() File "C:\Users\luwen\AppData\Local\Temp\pip-build-env-tqgiy54n\overlay\Lib\site-packages\setuptools\build_meta.py", line 512, in run_setup super().run_setup(setup_script=setup_script) File "C:\Users\luwen\AppData\Local\Temp\pip-build-env-tqgiy54n\overlay\Lib\site-packages\setuptools\build_meta.py", line 317, in run_setup exec(code, locals()) File "<string>", line 392, in <module> File "<string>", line 371, in setup_package File "C:\Users\luwen\AppData\Local\Temp\pip-install-e2roc4mf\numpy_c24b12f813b24ec6b8031ec1b4ffb42f\numpy\distutils\__init__.py", line 8, in <module> from . import ccompiler File "C:\Users\luwen\AppData\Local\Temp\pip-install-e2roc4mf\numpy_c24b12f813b24ec6b8031ec1b4ffb42f\numpy\distutils\ccompiler.py", line 17, in <module> from numpy.distutils import log File "C:\Users\luwen\AppData\Local\Temp\pip-install-e2roc4mf\numpy_c24b12f813b24ec6b8031ec1b4ffb42f\numpy\distutils\log.py", line 13, in <module> from numpy.distutils.misc_util import (red_text, default_text, cyan_text, File "C:\Users\luwen\AppData\Local\Temp\pip-install-e2roc4mf\numpy_c24b12f813b24ec6b8031ec1b4ffb42f\numpy\distutils\misc_util.py", line 15, in <module> from distutils.msvccompiler import get_build_architecture ModuleNotFoundError: No module named 'distutils.msvccompiler' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
最新发布
08-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值