[CF813B] The Golden Age(数学,枚举,溢出trick)

本文解析了CodeForces上的一道题目,该题要求找出指定区间内满足特定条件的最长子序列。通过分析,发现指数运算中的指数不会太大,并提供了一种枚举所有可能值并求解的方法。

题目链接:http://codeforces.com/contest/813/problem/B

题意:给定x,y,l,r,求[l,r]区间内满足n!=x^a+y^b的最长子序列的长度。2<=x,y<=1e18

稍微分析下会发现,这个x^a,y^b中a、b不会太大,因为2^63大约为1e19。

枚举所有x^a, y^b,再求和就可以。

但是可能会溢出,要稍微抠一下才能过。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef unsigned long long LL;
 5 const int maxn = 1100;
 6 LL x, y, l, r;
 7 LL X[maxn], Y[maxn];
 8 vector<LL> ret;
 9 int n, m;
10 
11 int main() {
12     // freopen("in", "r", stdin);
13     while(cin >> x >> y >> l >> r) {
14         memset(X, 0, sizeof(X));
15         memset(Y, 0, sizeof(Y));
16         ret.clear();
17         X[0] = 1, Y[0] = 1;
18         X[1] = x, Y[1] = y;
19         for(n = 2; x * X[n-1] >= 0 && x * X[n-1] <= (LL)1e18; n++) {
20             if(X[n-1] % x != 0 || X[n-1] == 0) break;
21             X[n] = x * X[n-1];
22         }
23         for(m = 2; y * Y[m-1] >= 0 && y * Y[m-1] <= (LL)1e18; m++) {
24             if(Y[n-1] % y != 0 || X[n-1] == 0) break;
25             Y[m] = y * Y[m-1];
26         }
27         n--; m--;
28         for(int i = 0; i <= n; i++) {
29             for(int j = 0; j <= m; j++) {
30                 if((i==0||X[i]%x==0)&&(j==0||Y[j]%y==0)) {
31                     if(l <= X[i] + Y[j] && X[i] + Y[j] <= r) {
32                         ret.push_back(X[i]+Y[j]);
33                     }
34                 }
35             }
36         }
37         ret.push_back(l-1); ret.push_back(r+1);
38         sort(ret.begin(), ret.end()); ret.erase(unique(ret.begin(), ret.end()), ret.end());
39         // for(auto x : ret) cout << x << " ";
40         // cout << endl;
41         LL seg = 0;
42         for(int i = 1; i < ret.size(); i++) {
43             seg = max(seg, ret[i]-ret[i-1]-1);
44         }
45         cout << seg << endl;
46     }
47     return 0;
48 }

 

转载于:https://www.cnblogs.com/kirai/p/6956483.html

(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、付费专栏及课程。

余额充值