看题传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1496

题目大意:

给定a,b,c,d。a*x1^2+b*x2^2+c*x3^2+d*x4^2=0

其中x1~x4 在 [-100,100]区间内, a,b,c,d在[-50,50] 区间内。

求满足上面那个式子的所有解的个数。

思路:

这题用hash的思想很巧妙,先对x1和x2进行枚举,存在的存进hash表中,然后接下来枚举x3和x4,如果恰好和前面的为相反数,那么答案+上前面出现的次数.

提高效率的方法:

1.用枚举1~100而负半区域不考虑,节省枚举数,最后答案因为四个数全部都是正的,而实际上都有每个数都有正有负,故答案*16

2.把平方运算结果存下来。

3.位运算优化hash取模

4.同号的剪枝

普通的hash:

#include<cstdio>
#include<cstring>
const int MAXN=50*100*100*2+10;
int hash_pos[MAXN]; //positive
int hash_neg[MAXN]; //negative
int res[101];
int main()
{
int a,b,c,d ;
for(int i=1;i<=100;i++)
res[i]=i*i; while(~scanf("%d%d%d%d",&a,&b,&c,&d))
{
if(a>0 && b>0 && c>0 && d>0||a<0 && b<0 && c<0 && d<0)
{
printf("0\n");
continue;
}
memset(hash_pos,0,sizeof(hash_pos));
memset(hash_neg,0,sizeof(hash_neg)); for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
int x=res[i]*a+res[j]*b;
if(x >=0)
hash_pos[x]++;
else
hash_neg[-x]++;
}
} int cnt=0;
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
int x=res[i]*c+res[j]*d;
if(x >0)
cnt+=hash_neg[x];
else
cnt+=hash_pos[-x];
}
} printf("%d\n",cnt<<4);
}
return 0;
}

采用开散列+位运算优化的取模运算!

//0 MS 476K
//By hr_whisper 2013/12/27
#include<cstdio>
#include<cstring>
const int mod=1<<15;
struct edge
{
int val,next,cnt;
}edge[mod]; int head[mod];
int len=0; inline int gethash(int x)
{
return (x+ mod) & (mod-1);
} inline void insert(int x)
{
int id=gethash(x);
for(int i=head[id]; i != -1;i=edge[i].next)
{
if(edge[i].val==x)
{
edge[i].cnt++;
return;
}
}
edge[len].cnt=1;
edge[len].next=head[id];
edge[len].val=x;
head[id]=len++;
} inline int search(int x)
{
int id=gethash(x);
for(int i=head[id] ; i!=-1;i=edge[i].next)
{
if(edge[i].val==x)
return edge[i].cnt;
}
return 0;
}
int res[101]; int main()
{
int a,b,c,d ;
for(int i=1;i<=100;i++)
res[i]=i*i; while(~scanf("%d%d%d%d",&a,&b,&c,&d))
{
if(a>0 && b>0 && c>0 && d>0||a<0 && b<0 && c<0 && d<0)
{
printf("0\n");
continue;
} memset(head,-1,sizeof(head));
len=0; for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
int x=res[i]*a+res[j]*b;
insert(x);
}
} int cnt=0;
for(int i=1;i<=100;i++)
{
for(int j=1;j<=100;j++)
{
int x=res[i]*c+res[j]*d;
cnt+=search(-x);
}
} printf("%d\n",cnt<<4);
}
return 0;
}

HDU 1496 Equations hash HDU上排名第一!的更多相关文章

  1. hdu 1496 Equations hash表

    hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...

  2. hdu 1496 Equations

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 Equations Description Consider equations having ...

  3. HDU - 1496 Equations (hash)

    题意: 多组测试数据. 每组数据有一个方程 a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0,方程中四个未知数 x1, x2, x3, x4 ∈ [-100, 100], 且 ...

  4. HDU 1496 Equations(哈希表)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1496 [题目大意] 给出一个方程ax1^2+bx2^2+cx3^2+dx4^2=0,求-100到1 ...

  5. HDU 1496 Equations 等式(二分+暴力,技巧)

    题意:给出4个数字a,b,c,d,求出满足算式a*x1^2+b*x2^2+c*x3^2+d*x4^2=0的 (x1,x2,x3,x4) 的组合数.x的范围[-100,100],四个数字的范围 [-50 ...

  6. HDU 1496

    题目出处:HDU OJ 1496 http://acm.hdu.edu.cn/showproblem.php?pid=1496 为了练习Hash,特定采用了杭电自带的分类列表http://acm.hd ...

  7. hdu 1880 字符串hash

    /*普通的hsah 由于元素太多 空间很小..hash碰撞很厉害.30分*/ #include<iostream> #include<cstdio> #include<c ...

  8. HDU ACM 1496 Equations

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. Equations(hdu 1496 二分查找+各种剪枝)

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. 体验 Windows 系统 CVM

    添加角色功能: service.msc 设置自动服务,net start telnet

  2. umask---默认权限掩码

  3. 小米开源文件管理器MiCodeFileExplorer-源码研究(2)-2个单实例工具类

    从本篇开始,讲解net.micode.fileexplorer.util工具包中的类.这个包下的类,功能也比较单一和独立.很多代码的思想和实现,可以用于JavaWeb和Android等多种环境中. 一 ...

  4. 一个Web报表项目的性能分析和优化实践(七):性能监测工具JavaMelody

    简介 JavaMelody 能够监测Java或Java EE应用程序服务器,并以图表的方式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求.sql请 ...

  5. 【Codeforces Round #426 (Div. 2) C】The Meaningless Game

    [Link]:http://codeforces.com/contest/834/problem/C [Description] 有一个两人游戏游戏; 游戏包括多轮,每一轮都有一个数字k,赢的人把自己 ...

  6. Oracle Sqlplus中上下键出现^[[A乱码问题

    安装rlwrap  下载:http://utopia.knoware.nl/~hlub/uck/rlwrap/ 或者 百度云盘:http://pan.baidu.com/s/1ntM8YXr 须要先安 ...

  7. tab标签页(选项卡)插件

    <body style="margin: 50px;"> <ul id="nav" class="nav nav-tabs" ...

  8. Hp Open View安装使用视频

    去年完成的cisco works 2000操作(http://chenguang.blog.51cto.com/blog/350944/124729)视频深受广大博友欢迎许多人来信咨询,最近刚做完一个 ...

  9. 记Bootstrap Table两种渲染方式

    这里主要区别两种Bootstrap Table的数据渲染方式,一.属性渲染方式,二.JS渲染方式 工作直接接手前人的,之前都直接在table标签上渲染属性,后面因为项目需要,同一页面的表格,需要申请不 ...

  10. vue中添加favicon

    基于vue-cli 2 首先将favicon.ico图片放在根目录下,通过以下两种方法使其显示正确. 方法一:修改index.html文件 <link rel="shortcut ic ...