题目

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]上非常稀疏,故可以使用二分查找或者哈希查找来减少空间复杂度。

感想

下次提交前应当先算清空间复杂度,而不是直接改。

代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <sstream>
  6. using namespace std;
  7. typedef long long ll;
  8. const int maxn = 5;
  9. const int base = 50 * 50 * 50 * 50 * 2;
  10. const int maxm = base * 2;
  11. int a[maxn];
  12. short s34[maxm];
  13.  
  14. int solve(){
  15. int cnt = 0;
  16. for(int x3 = -50;x3 <= 50;x3++){
  17. if(x3 == 0)continue;
  18. int s3 = a[3] * x3 * x3 * x3;
  19. for(int x4 = -50;x4 <= 50;x4++){
  20. if(x4 == 0)continue;
  21. int s4 = a[4] * x4 * x4 * x4;
  22. s34[s3 + s4 + base]++;
  23. }
  24. }
  25. for(int x0 = -50;x0 <= 50;x0++){
  26. if(x0 == 0)continue;
  27. int s0 = a[0] * x0 * x0 * x0;
  28. for(int x1 = -50;x1 <= 50;x1++){
  29. if(x1 == 0)continue;
  30. int s1 = a[1] * x1 * x1 * x1;
  31. for(int x2 = -50;x2 <= 50;x2++){
  32. if(x2 == 0)continue;
  33. int s2 = a[2] * x2 * x2 * x2;
  34. int sum = s0 + s1 + s2;
  35. if (base - sum >= 0 && base - sum < maxm){
  36. cnt += s34[base - sum];
  37. }
  38. }
  39. }
  40. }
  41. return cnt;
  42. }
  43.  
  44. int main(){
  45. #ifdef LOCAL
  46. freopen("input.txt","r",stdin);
  47. #endif // LOCAL
  48. for(int i = 0;i < maxn;i++)scanf("%d", a + i);
  49. printf("%d\n", solve());
  50. return 0;
  51. }

POJ 1840 Eqs 解方程式, 水题 难度:0的更多相关文章

  1. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  2. UVa 10970 - Big Chocolate 水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  3. POJ 2002 Squares 几何, 水题 难度: 0

    题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...

  4. POJ 1936 All in All 匹配, 水题 难度:0

    题目 http://poj.org/problem?id=1936 题意 多组数据,每组数据有两个字符串A,B,求A是否是B的子串.(注意是子串,也就是不必在B中连续) 思路 设置计数器cnt为当前已 ...

  5. hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0

    H - National Day Parade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  6. UVa 10340 - All in All 水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  7. UVa 3602 - DNA Consensus String 水题 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  8. UVa LA 3213 - Ancient Cipher 水题 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  9. UVa 11039 - Building designing 贪心,水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

随机推荐

  1. Hexo 的next主题下添加网易云音乐作BGM

    首先,你要看看你选中的歌能不能在网页版的网易云音乐生成外链,因为版权保护原因,有些音乐是生不成外链的,比如这样的: 所以,选些可以生成外链的音乐.生成对应的外链 比如这里的重点是HTML代码中的src ...

  2. 学习笔记28—Python 不同数据类型取值方法

    1.array数据类型 1)-------> y[i,] 或者 y[i] 2.遍历目录下所有文件夹: def eachFile(filepath):     pathDir =  os.list ...

  3. 常用命令-python篇

    1. pip 加速命令 pip install --index-url https://pypi.douban.com/simple pip install -i https://pypi.tuna. ...

  4. 猫眼电影爬取(二):requests+beautifulsoup,并将数据存储到mysql数据库

    上一篇通过requests+正则爬取了猫眼电影榜单,这次通过requests+beautifulsoup再爬取一次(其实这个网站更适合使用beautifulsoup库爬取) 1.先分析网页源码 可以看 ...

  5. (转)C# Textbox的ImeMode取值对中文输入法的影响

    取值 五笔加加 微软拼音3.0 搜狗拼音 说明 NoControl 首次调出后按一次ctrl+space才能正确使用 中西标点或全半角字符继承上次设置 调出后默认为英文输入状态  调出后默认为西文标点 ...

  6. WCF初见之SQL数据库的增删改查

    1.首先要连接数据库,自然要有数据库啦,创建一个数据库表Login,并插入一个数据: --创建数据库表login CREATE TABLE Login ( UName VARCHAR(20) PRIM ...

  7. php正则的使用

    函数 描述 preg_filter 执行一个正则表达式搜索和替换 preg_grep 返回匹配模式的数组条目 preg_last_error 返回最后一个PCRE正则执行产生的错误代码 preg_ma ...

  8. 数据库SQLserver(课本)

    一.SQL server的部署 1.数据库的基本概念 数据库通常是一个由行和列组成的二维表 数据表中的行通常叫做记录或元祖 数据表中的列通常叫做字段或属性 2.主键和外键 主键:定义主键可以保证数据的 ...

  9. 2.5 UML顺序图

    相关概念 交互 对象之间为实现某一功能而必须实施的协作过程.动态行为,称为交互 消息 对象间的协作与交流表现为一个对象以某种方式启动另一个对象的活动,这种交流在 UML里被定义为消息 顺序图的建模元素 ...

  10. gitlab 集成Jenkins

    项目:使用git+jenkins实现持续集成 开始构建  General  源码管理 我们安装的是Git插件,还可以安装svn插件  我们将git路径存在这里还需要权限认证,否则会出现error  我 ...