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 ...
随机推荐
- 基于Arcface 免费离线人脸识别 2.0 Demo C#
本来打算做个C#版demo,但没用成功.使用虹软最新人脸识别技术开发完成 过程如下: 1. 传入一张单人脸照片: 2.调用检测人脸函数ASFDetectFaces,成功返回人脸信息的指针: 3.使用 ...
- js几个小技巧和坑
蝴蝶书看了,也知道充满了毒瘤和糟粕,但该用还是得用. 实际写了几天,小技巧记录下来.都是在py里有直接答案,不会遇到的问题,没想到js里这么费事. 还是要多读<ES6标准入门> 1判断ob ...
- unbuntu安装Node.js
在官网https://nodejs.org/en/下载 手动创建链接的话,新安装的angular的ng typescript的tsc都要自己手动建立软链接,要不就每个工程里npm install一 ...
- 牛客小白月赛7 B 自杀游戏
自杀游戏 思路: sg函数 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include&l ...
- Codeforces 1006 F - Xor-Paths
F - Xor-Path 思路: 双向搜索dfs 如果普通的搜索复杂度是n 那么双向搜索复杂度是√n 代码: #include<bits/stdc++.h> using namespace ...
- 优雅地记录Python程序日志1:logging模块简介
本文摘自:https://zhuanlan.zhihu.com/p/31893724 本篇涉及: logging模块的调用: 保存log日志为文件: 调整输入日志等级: 修改日志消息格式: 前言 在使 ...
- java8新特性(二)_lambda表达式
最近一直找java8相关新特性的文章,发现都太没有一个连贯性,毕竟大家写博客肯定都有自己的侧重点,这里找到一本书,专门介绍java8新特性的,感觉大家可以看看<写给大忙人看的JavaSE8> ...
- Linux下安装 jdk
转自 http://www.cnblogs.com/shihaiming/p/5809553.html 0.下载jdk8 登录网址:http://www.oracle.com/technetwork/ ...
- Confluence 6 指派空间权限概念
如果你是一个空间的管理员,你可以对空间的使用权限进行控制.你可以为独立用户或者Confluence Groups的权限进行权限的指派/收回. Confluence 管理员可以将用户分配到用户组中,这样 ...
- 集合 (set)
set 是一个无序不重复的元素集,集合跟字典是无序的,不支持索引 创建集合: 第一种方式:通过{ }创建 >>> num={1,2,3,4,3,3,1} >>> n ...