POJ 1840 Eqs 解方程式, 水题 难度:0
题目
http://poj.org/problem?id=1840
题意
给
与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0] *
pow(x[0], 3) + a[1] * pow(x[1], 3) + a[2] * pow(x[2], 3) + a[3] *
pow(x[3], 3) + a[4] * pow(x[4], 3)==0
其中x[i]满足-50<=x[i]<=50,0<=i<5
思路
该等式明显可以转化为a[0] * pow(x[0], 3) + a[1] * pow(x[1], 3) + a[2] * pow(x[2], 3) == -(a[3] * pow(x[3], 3) + a[4] * pow(x[4], 3))
所以很自然可以想到,可以先列举并存储等式右边的值及对应组数(状态数约为50 * 50 * 50 * 50 * 4,约为2e7),再列举左边的所有可能,状态数为100 * 100 * 100,即可知道总组数。
但原题目的空间限制使得开50 * 50 * 50 * 50 * 4个int型状态数组不可取,故此处改用short数组。
不过因为等式右边的值在[-50 * 50 * 50 * 50 * 2, +50 * 50 * 50 * 50 * 2]上非常稀疏,故可以使用二分查找或者哈希查找来减少空间复杂度。
感想
下次提交前应当先算清空间复杂度,而不是直接改。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;
typedef long long ll;
const int maxn = 5;
const int base = 50 * 50 * 50 * 50 * 2;
const int maxm = base * 2;
int a[maxn];
short s34[maxm]; int solve(){
int cnt = 0;
for(int x3 = -50;x3 <= 50;x3++){
if(x3 == 0)continue;
int s3 = a[3] * x3 * x3 * x3;
for(int x4 = -50;x4 <= 50;x4++){
if(x4 == 0)continue;
int s4 = a[4] * x4 * x4 * x4;
s34[s3 + s4 + base]++;
}
}
for(int x0 = -50;x0 <= 50;x0++){
if(x0 == 0)continue;
int s0 = a[0] * x0 * x0 * x0;
for(int x1 = -50;x1 <= 50;x1++){
if(x1 == 0)continue;
int s1 = a[1] * x1 * x1 * x1;
for(int x2 = -50;x2 <= 50;x2++){
if(x2 == 0)continue;
int s2 = a[2] * x2 * x2 * x2;
int sum = s0 + s1 + s2;
if (base - sum >= 0 && base - sum < maxm){
cnt += s34[base - sum];
}
}
}
}
return cnt;
} int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
for(int i = 0;i < maxn;i++)scanf("%d", a + i);
printf("%d\n", solve());
return 0;
}
POJ 1840 Eqs 解方程式, 水题 难度:0的更多相关文章
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- UVa 10970 - Big Chocolate 水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- POJ 2002 Squares 几何, 水题 难度: 0
题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...
- POJ 1936 All in All 匹配, 水题 难度:0
题目 http://poj.org/problem?id=1936 题意 多组数据,每组数据有两个字符串A,B,求A是否是B的子串.(注意是子串,也就是不必在B中连续) 思路 设置计数器cnt为当前已 ...
- hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0
H - National Day Parade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- UVa 10340 - All in All 水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 3602 - DNA Consensus String 水题 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVa LA 3213 - Ancient Cipher 水题 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVa 11039 - Building designing 贪心,水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
随机推荐
- MYSQL常用函数(聚合函数(常用于GROUP BY从句的SELECT查询中))
AVG(col)返回指定列的平均值 COUNT(col)返回指定列中非NULL值的个数 MIN(col)返回指定列的最小值 MAX(col)返回指定列的最大值 SUM(col)返回指定列的所有值之和 ...
- Lua和C++交互 学习记录之七:C++全局函数注册为Lua模块
主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3 参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...
- Codeforces Gym - 101102B - The Little Match Girl
B. The Little Match Girl time limit per test 1 second memory limit per test 256 megabytes input stan ...
- angular2 学习笔记 (Typescript - Attribute & reflection & decorator)
更新 : 2018-11-27 { date: Date } 之前好像搞错了,这个是可以用 design:type 拿到的 { date: Date | null } 任何类型一但配上了 | 就 de ...
- gradlew 的https代理设定
在内网编译vlc for Android 时, 总是在 [./gradlew assemble] 卡住, 在网上找到了设置代理的方法: 在gradlew 的同一目录,建立一个 gradle.prope ...
- 管家基因 | Human housekeeping genes
管家基因就是在细胞里稳定表达的基因,及时在胁迫状态下,表达的差异也不大. 以前做实验的时候就经常听说管家基因,因为在做RT-PCR的时候需要同时检测管家基因,这样可以用于矫正我们不同批次的结果. Li ...
- NexT 个性化设置
NexT 主题添加分类页面 新建页面 在本地使用终端 cd 到 blog 文件夹下,执行如下命令: $ cd Documents/blog $ hexo new page categories 设置页 ...
- linux文件管理之proc文件系统
proc 文件系统 ==================================================================================== Linux ...
- jQuery -- touch事件之滑动判断(左右上下方向)
$("body").on("touchstart", function(e) { // 判断默认行为是否可以被禁用 if (e.cancelable) { // ...
- Xpath做数据解析
xpath是一个路径表达式, xpath学习 (1)xpath节点 在XPath中,有七种类型的节点:元素,属性,文本,命名空间,处理指令,注释以及文档节点:XML文档是被作为节点树来对待的.树的根被 ...