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的更多相关文章

随机推荐

  1. Fedora 19 配置参考

    1. 安装完Fedora 19之后,第一件事不是升级系统,而是添加源. 下载我配置好的源,非常全面,适用Fedora 19 x86_64.点我下载 打开终端,切换到repo.zip所在目录:mv re ...

  2. Objective-C中字典的使用方法总结

    在Objective-C中提供了两个类来实现字典,即NSDictionary和NSMutableDictionary.其中,NSMutableDictionary是NSDictionary的子类,它继 ...

  3. HTML5-draggable(拖放)

                                                   <!DOCTYPE html> <html class="no-js" ...

  4. windows bat脚本实现ftp自动下载 删除

    现在有一个需求就是把远程某个文件下面的图片,下载到本地,并且删除下载成功的的文件,而且远程目录下的那个图片会随时增加.假设一下如果所有的脚本都写好了,那么就需要调用windows上的计划任务定时执行脚 ...

  5. TextView中gravity属性值测定

    Attributes Explain top 不改变控件大小,对齐到容器顶部 bottom 不改变控件大小,对齐到容器底部 left 不改变控件大小,对齐到容器左侧 right 不改变控件大小,对齐到 ...

  6. .NET Framework 3.5 安装错误:0x800F0906、0x800F081F、0x800F0907

    使用Add-WindowsFeature 照成的问题 I get the failure below..  If I pick the Server 2012 R2 image from 8/15/2 ...

  7. 定位表的数据块并且dump出来

    SQL> select * from city;         ID NAME ---------- ----------          7 Chicago          6 Jers ...

  8. android LayoutInflater.inflate()的参数介绍

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  9. Swift学习:闭包(Closures)

    /* 闭包(Closures)* 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值.* 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambdas类 ...

  10. nginx error_page 404 用 php header 无法跳转

    nginx error_page 404 用 php header 无法跳转 之前用Apache的时候,只需要设置 ErrorDocument 404 /404.php 就可以在 404.php 中根 ...