题目

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 LOCALfreopen("input.txt","r",stdin);
#endif // LOCALfor(int i = 0;i < maxn;i++)scanf("%d", a + i);printf("%d\n", solve());return 0;
}

转载于:https://www.cnblogs.com/xuesu/p/6382400.html

POJ 1840 Eqs 解方程式, 水题 难度:0相关推荐

  1. POJ 1936 字符匹配(水题)

    题目链接: http://poj.org/problem?id=1936 题目大意: 给定字符a,b,问b中去掉一些字符后能不能得到a 解题思路: 暴力从前往后扫描一遍即可. AC代码: /*** @ ...

  2. poj 1840 Eqs

    题目连接 http://poj.org/problem?id=1840 Eqs Description Consider equations having the following form:  a ...

  3. poj 3095 Linear Pachinko 模拟水题

    题意: 给一个字符串,求小球随机放在上面进洞或到达两边之外的期望. 分析 水题,直接模拟. 代码: //poj 3095 //sep9 #include <iostream> using ...

  4. 【POJ 2503】Babelfish(水题)stl map存取即可

    题目链接 题目链接 http://poj.org/problem?id=2503 题意 英文A <=> 方言B 输入B,求A Code(G++) #include <iostream ...

  5. POJ 3087 Shuffle'm Up(水题)

    题目链接 蒙姐跟我说了题意,就很简单了,类似打扑克中的插牌.磕磕绊绊,终于把POJ第三次训练刷完了. 1 #include <cstdio> 2 #include <cstring& ...

  6. POJ 2017 No Brainer(超级水题)

    一.Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indi ...

  7. poj 1789 kruscal水题

    继续水水题... 题目:http://poj.org/problem?id=1789 把车看成结点,车之间的距离看作权重就是一个图了,然后求最小生成树... 的确水题,但看题目花了挺长时间,不知道如果 ...

  8. 水题/poj 1852 Ants

    1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std ...

  9. poj 2388 排序的水题

    纯纯的水题. #include <iostream> #include <fstream> #include <cstdlib>using namespace st ...

最新文章

  1. anaconda: import numpy报错:ImportError: DLL load failed: 找不到指定的模块。
  2. Play和Grails Java框架的优缺点
  3. php没有上级分类的联动,ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单...
  4. canvas 实现雷达图
  5. OpenCV4每日一练day6:Image Watch的使用
  6. 问题 B: 编写函数:Swap (I) (Append Code)
  7. python学到什么程度可以找到工作-Python学到什么程度可以面试工作?
  8. Wireshark实战分析之DHCP协议(一)
  9. vs2015安装msdn_visual studio 2015离线版msdn下载和安装
  10. 介绍一下Win11单独设置耳机音量的方法
  11. html table 美化,html如何用css美化表格
  12. 嵌入式Linux开发|点亮那颗LED灯
  13. android wifi认证,android 怎么检测连接的wlan wifi需要portal认证
  14. HLS、Smooth Streaming、HDS和Dash
  15. JVM - 垃圾回收(垃圾标记阶段算法,内存泄漏与溢出)(2)
  16. RS485学习(一)
  17. 通达OA11.6复现
  18. iOS音效和音乐播放
  19. 千梦网创108计第三十六计:当地人才招聘网,一个年入50W的战友实操案例
  20. 【网络爬虫项目】实战知识点 - webcrawler

热门文章

  1. linux中yum怎么安装服务器,yum安装(linux如何安装yum)
  2. oracle 查重复_日常答疑|MySQL删除重复数据踩过得坑
  3. 关于php的cgi,php-fpm的关系
  4. php中的interface和implements及其他
  5. 【深度学习】Squeeze-and-Excitation (SE) 模块优势解读
  6. java方法的参数_Java方法参数
  7. 计算机编程免费ppt,计算机编程与C概述课件.ppt
  8. 718保时捷spyder_我要买保时捷718 Spyder的原因:新手也可以玩手动
  9. 网络推广——如何在网站内容优化工作中实现更好的网络推广?
  10. 营销型网站优化攻略分享