POJ1840Eps
http://poj.org/problem?id=1840
题意 : 有这样一个式子a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,给你五个系数的值,让你找出x1,x2,x3,x4,x5的值满足这个式子,满足这个式子的方案有多少种输出
思路 : 这个题的话我一开始想的就是暴搜,五个for循环,但肯定会超时啊,问了会神才知道,原来这个题变通一下就行了,既然五个for循环超时那就分开,两个和三个,a1x13+ a2x23+ a3x33= -(a4x43+ a5x53),这样去搜就可以了,哈希表存一下,还有,这个的话,若x4和x5系数和x都是50,那么50*50*50*50+50*50*50*50就等于1250万,再加上负数,所以数组就要开到2500万,用int就会超内存,唉,多么痛的领悟啊!我交了两遍呢,所以用short定义
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = ;
short ch[maxn] ;
int main()
{
int a,b,c,d,e ;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
int sum = ;
memset(ch,,sizeof(ch));
for(int x1 = - ; x1 <= ; x1++)
{
if( x1 == )
continue ;
for(int x2 = - ; x2 <= ; x2++)
{
if(x2 == )
continue ;
for(int x3 = - ; x3 <= ; x3++)
{
if(x3 == )
continue ;
sum = a*x1*x1*x1+b*x2*x2*x2+c*x3*x3*x3 ;
if(sum < )
sum += maxn ;
ch[sum] ++ ;
}
}
}
int count = ;
for(int x4 = - ; x4 <= ; x4++)
{
if(x4 == )
continue ;
for(int x5 = - ; x5 <= ; x5++)
{
if(x5 == )
continue ;
sum = d*x4*x4*x4+e*x5*x5*x5 ;
if(sum < )
sum += maxn ;
count += ch[sum] ;
}
}
printf("%d\n",count) ;
return ;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define MAXN 25000001
using namespace std;
int main()
{
int a1,a2,a3,a4,a5;
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
map<int,int>q;
for(int x1=-; x1<=; x1++)
{
if(!x1) continue;
for(int x2=-; x2<=; x2++)
{
if(!x2) continue;
int sum=a1*x1*x1*x1+a2*x2*x2*x2;
if(q.find(sum)==q.end())
q.insert(pair<int,int>(sum,));
else q[sum]++;
}
}
int ans=;
for(int x3=-; x3<=; x3++)
{
if(!x3) continue;
for(int x4=-; x4<=; x4++)
{
if(!x4) continue;
for(int x5=-; x5<=; x5++)
{
if(!x5) continue;
int sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
if(q.find(sum)==q.end()) continue;
ans+=q[-sum];
}
}
}
printf("%d\n",ans);
return ;
}
下面这个是会神用map写的
POJ1840Eps的更多相关文章
随机推荐
- MySQL事务机制
事务机制的特性通常被概括为"ACID原则" A(Atomic) 原子性: 构成一个事务的所有语句应该是一个独立的逻辑单元,要么全部执行成功, 要么一个都不成功, 你不能只执行他们当 ...
- SQL 面试题(一)
问题来自于CSDN问答,练练SQL吧. 测试数据SQL代码: if OBJECT_ID('td_ls_2') is not null drop table td_ls_2 go if OBJECT_I ...
- 33选6算法:M个数N个为一组,无重复的排列组合
private void button1_Click(object sender, EventArgs e) { int nCnt = 0; List nNumList = new List(); f ...
- MongoDB 备份方法
翻译自 http://docs.mongodb.org/manual/core/backups/ 有以下几种方法来备份MongoDB群集: 通过复制底层数据文件来备份 通过mongodump来备份 通 ...
- Hadoop上路-04_HBase0.98.0入门
以下操作在Hadoop分布式集群基础上进行. 一.分布式环境搭建 下载:)验证 3)修改%HBASE%/conf/hbase-env.sh 4)修改$HBASE_HOME/conf/hbase-sit ...
- PHP:parse_str()字符串函数
parse_str()-把字符串解析成多个变量. 描述:void parse_str(sring $str [, array $arr]) 如果str是URL传递入的查询字符串(query stri ...
- intellij idea 代码正常,但是编译出现 java:需要";"
1. 查看idea 的maven 插件是不是版本太低,发现应用里是用的jdk是1.7 maven插件是2.2.1 2. 查看应用编码,比如部分文件是GBK,里面有中文,而整个应用的编码是Utf-8
- 【转】 管理CPU 亲和性
简单地说,CPU 亲和性(affinity) 就是进程要在某个给定的 CPU 上尽量长时间地运行而不被迁移到其他处理器的倾向性.Linux 内核进程调度器天生就具有被称为 软 CPU 亲和性(affi ...
- DataTable中如何去除重复的项【转】
上周在项目中遇到一个问题,就是获取DataTable中某一列的值,因为从数据库中检索数据时,按照2个字段进行分组,而要获得的那一列刚好在分组这两列中,所以该列的值必然有重复,于是就想到了去除重复,有了 ...
- 怎样在cmd(命令提示符)下进行复制粘贴操作
如右图,右键命令提示符窗口的标题栏,选择属性. 选择“编辑选项”里的“快速编辑模式”,并确定之: 在弹出的应用选择提示框上选择“保存属性,供以后具有相同标题的窗口使用”: 如此你就可以 ...