P17736 [ICPC 2019 Xuzhou R] F题题解

· · 题解

P17736 [ICPC 2019 Xuzhou R] F题题解

解法说明本题解使用打表方法解题

题意梗概

对于 x \in [0,200] 找到任意一组a,b,c \in [-5000,5000] 满足 a^3 + b^3 + c^3 = x

预处理分析

分析 x 和 a , b的取值范围,显然,朴素的枚举无法通过其时间限制,所以针对其较小的答案范围,我们尝试打表。

打表,是一个信息学专用术语,意指对一些题目,通过打表技巧获得一个有序表或常量表,来执行程序某一部分,优化时间复杂度。这种算法也可用于在对某种题目没有最优解法时,用来得到分数的一种策略。——百度百科

正式解题

1. 打表程序部分

如果仅使用 for 时间复杂度为O(mn^3) 显然无法承受。 所以选择 unordered_map ,枚举 a 和 b ,把每一组 a^3 + b^3 的值作为键,a,b 存储为 pair 作为值。 再枚举 x 和 c ,查询键为 x - c^3的值,存储三元组即可。 实现如下 :::success[code]


#include <bits/stdc++.h>
using namespace std;
unordered_map<long long,pair<int,int>>mp;
#define int long long 
signed main()
{
for(int a = -5000;a <= 5000;a++){
for(int b = -5000;b <= 5000;b++){
int x = a*a*a + b*b*b;
if(mp.find(x) == mp.end()){
mp[x] = {a,b};
}
}
}
for(int i = 0;i <= 200;i++){
bool flag = 1;
for(int c = -5000;c <= 5000;c++){
int ans = i -c*c*c;
if(mp.find(ans) != mp.end()){
flag = 0;
int b = mp[ans].second;
int a  = mp[ans].first;
cout << a<<","<< b<<","<<c<<",";
break;
}
}
if(flag)
cout << -1 <<","<< -1<<","<<-1<<",";
}
return 0;
}```
:::
#### 2.提交部分
无需额外说明
:::success[code]
:::success[code]
```cpp  line-numbers
int main(){
int t;
cin >> t;
while(t--){
int x;
cin >> x;
if(ans[x][0]==-5001){
cout << "impossible\n";
}
else{
cout << ans[x][0] << " " << ans[x][1] << " " << ans[x][2]<<"\n";
}
}
return 0;
}
}```
:::
---
附录
打表文件
:::success[code]
```cpp  line-numbers
int ans[][3]={
0,0,0,
0,0,1,
0,1,1,
1,1,1,
-5001,0,0,
-5001,0,0,
-1,-1,2,
0,-1,2,
0,0,2,
0,1,2,
1,1,2,
297,-641,619,
7,-11,10,
-5001,0,0,
-5001,0,0,
2,-1,2,
0,2,2,
1,2,2,
75,-218,215,
0,-2,3,
1,-2,3,
28,-86,85,
-5001,0,0,
-5001,0,0,
2,2,2,
1839,-2683,2357,
0,-1,3,
0,0,3,
0,1,3,
1,1,3,
-5001,0,0,
-5001,0,0,
-5001,0,0,
-5001,0,0,
2,-1,3,
0,2,3,
1,2,3,
0,-3,4,
1,-3,4,
-5001,0,0,
-5001,0,0,
-5001,0,0,
-5001,0,0,
2,2,3,
-5,-7,8,
2,-3,4,
3,-2,3,
6,-8,7,
-2,-2,4,
-5001,0,0,
-5001,0,0,
602,-796,659,
-5001,0,0,
3,-1,3,
0,3,3,
1,3,3,
0,-2,4,
1,-2,4,
-5001,0,0,
-5001,0,0,
-1,-4,5,
0,-4,5,
2,3,3,
0,-1,4,
0,0,4,
0,1,4,
1,1,4,
-5001,0,0,
-5001,0,0,
2,-4,5,
11,-21,20,
2,-1,4,
0,2,4,
1,2,4,
-5001,0,0,
-5001,0,0,
-5001,0,0,
-5001,0,0,
26,-55,53,
-19,-33,35,
2,2,4,
3,3,3,
847,-1317,1188,
3,-2,4,
-5001,0,0,
-5001,0,0,
-5001,0,0,
-1972,-4126,4271,
3,-4,5,
6,-7,6,
3,-1,4,
0,3,4,
1,3,4,
-5,-5,7,
-5001,0,0,
-5001,0,0,
14,-22,20,
17,-22,18,
0,-3,5,
2,3,4,
-3,-6,7,
4,-3,4,
118,-239,229,
-5001,0,0,
-5001,0,0,
-4,-7,8,
2,-3,5,
947,-1309,1117,
-948,-1165,1345,
146,-1019,1018,
-5001,0,0,
148,-1040,1039,
-5001,0,0,
-5001,0,0,
-5001,0,0,
8,-12,11,
-1,-2,5,
0,-2,5,
3,3,4,
-2,-6,7,
4,-2,4,
-5001,0,0,
-5001,0,0,
-1,-1,5,
0,-1,5,
0,0,5,
0,1,5,
1,1,5,
0,4,4,
1,4,4,
-5001,0,0,
-5001,0,0,
2,-1,5,
0,2,5,
1,2,5,
2,-6,7,
2,4,4,
-9,-11,13,
-77,-86,103,
-5001,0,0,
-5001,0,0,
2,2,5,
-3,-7,8,
-5001,0,0,
3,-2,5,
-7,-8,10,
108,-149,127,
1528,-2366,2131,
-5001,0,0,
-5001,0,0,
260,-367,317,
3,-1,5,
0,3,5,
1,3,5,
3,-6,7,
3,4,4,
-5001,0,0,
-5001,0,0,
-5001,0,0,
80,-130,119,
2,3,5,
20,-31,28,
4,-3,5,
226,-1134,1131,
-45,-47,58,
-5001,0,0,
-5001,0,0,
-5001,0,0,
56,-172,170,
0,-7,8,
1,-7,8,
67,-87,71,
-5001,0,0,
-5001,0,0,
7,-8,7,
-5001,0,0,
-5001,0,0,
2,-7,8,
-10,-13,15,
3,3,5,
-5001,0,0,
4,-2,5,
9,-14,13,
10,-17,16,
-5001,0,0,
-5001,0,0,
5,-4,5,
27,-58,56,
4,-1,5,
0,4,5,
1,4,5,
4,-6,7,
4,4,4,
-5001,0,0,
-5001,0,0,
-5001,0,0,
3,-7,8,
2,4,5,
148,-195,161,
15,-24,22,
73,-114,103};```
:::