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 ...
随机推荐
- IOException parsing XML document from class path resource [WebRoot/WEB-INF/applicationContext.xml];
parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io. ...
- Python全栈开发-Day5-常用模块学习
本节大纲: 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 pyyaml处理 configparser hashlib re正则 ...
- 20165327 2017-2018-2 《Java程序设计》第一周学习总结
第1章 Java入门 一.Java 的特点 简单 面向对象 平台无关 多线程:允许同时完成多个任务 动态:Java程序的基本组成单元就是类(有些类是自己编写的,有一些是从类库中引入的,而类又是运行时动 ...
- English Voice of <<All Of Me>>
"All Of Me"我的一切 [Verse 1:]What would I do without your smart mouth没有你的蜜语甜言,我该怎办Drawing me ...
- CCPC2017湘潭 1263 1264 1267 1268
1263 拉升一下就A了 #include <iostream> #include <vector> #include <algorithm> #include & ...
- Hadoop – The Definitive Guide Examples,,IntelliJ
IntelliJ Project for Building Hadoop – The Definitive Guide Examples http://vichargrave.com/intellij ...
- PHP 练习项目------歆语微博项目
一个简单微博项目,php+mysql+apache开发,个人购买资料的项目练习,适合新手练习. 测试账号:zhangqie 密码:123456 功能列表: 数据库增删改查, 图片上传 表情,@好友 ...
- SPL之Iterator(迭代器)接口
前言:SPL是用于解决典型问题(standard problems)的一组接口与类的集合. <?php /** * Class MyIterator * 在 PHP 中,通常情况下遍历数组使用 ...
- 牛客寒假算法基础集训营6 J-迷宫
题目请点这里 分析:这是一道BFS的模板题,构造一个队列,将每个满足条件的(不超过边界,不超过左右移动次数的限制)位置推入队列,如果不是障碍物且没到达过,就将可到达位置的个数加1 此外,注意这里的输入 ...
- ZOJ - 3661 pam
题意:给一个字符串,和每个字符代表的val,每个回文串的价值就是前半部分的val26进制%777777777,求价值第k小的回文串 题解:建个pam,然后dfs两边(0,1),统计价值sort一遍就好 ...