HDU 1496 Equations hash HDU上排名第一!
看题传送门:
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上排名第一!的更多相关文章
- hdu 1496 Equations hash表
hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...
- hdu 1496 Equations
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 Equations Description Consider equations having ...
- HDU - 1496 Equations (hash)
题意: 多组测试数据. 每组数据有一个方程 a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0,方程中四个未知数 x1, x2, x3, x4 ∈ [-100, 100], 且 ...
- HDU 1496 Equations(哈希表)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1496 [题目大意] 给出一个方程ax1^2+bx2^2+cx3^2+dx4^2=0,求-100到1 ...
- 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 ...
- HDU 1496
题目出处:HDU OJ 1496 http://acm.hdu.edu.cn/showproblem.php?pid=1496 为了练习Hash,特定采用了杭电自带的分类列表http://acm.hd ...
- hdu 1880 字符串hash
/*普通的hsah 由于元素太多 空间很小..hash碰撞很厉害.30分*/ #include<iostream> #include<cstdio> #include<c ...
- HDU ACM 1496 Equations
Equations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Equations(hdu 1496 二分查找+各种剪枝)
Equations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- jdbc的数据库驱动类DriverManager.getConnection()详解
1.Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); ...
- css3--根据数据加载显示的一个动画
css: .circle { width: 200px; height: 200px; position: absolute; border-radius: 50%; background: #0cc ...
- JavaScript学习总结(9)——JS常用函数(一)
本文中,收集了一些比较常用的Javascript函数,希望对学习JS的朋友们有所帮助. 1. 字符串长度截取 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- ArcGIS中数据之间的转换接口IFeatureDataConverter2
之前我写过一篇文章关于ArcGIS各种空间数据格式之间转换的通用方法:ArcGIS中sde,mdb,shp数据之间的转换.这里使用的主要接口方法就是用到了IFeatureDataConverter接口 ...
- lightSlider 好图片轮播插件 支持移动端
http://jquery-plugins.net/jquery-lightslider-lightweight-responsive-content-slider https://github.co ...
- stm8开发环境配置及测试
需要准备的软件,硬件,IAR for stm8 (EWSTM8).stm8s标准固件库.ST-LINK.STM8s003f3核心板 安装IAR(其中包括st-link的驱动), 到这个网址下载stm8 ...
- Spring中的AOP注解方式和XML方式
应掌握内容:1. AOP的全名2. AOP的实现原理[静态代理和动态代理]3. 注解方式的配置4. 通知类型 A. 每种通知的特点和使用方式 B. 获取各种数据,方便日后操作5. 执行表 ...
- div的标准盒模型和怪异盒模型
(1)标准盒模型 标准盒模型宽高不会被padding和margin撑开 (2)怪异盒模型 怪异盒模型宽高会被padding和margin撑开
- codeforces 666E. Forensic Examination(广义后缀自动机,Parent树,线段树合并)
传送门: 解题思路: 很坑的一道题,需要离线处理,假如只有一组询问,那么就可以直接将endpos集合直接累加输出就好了. 这里就要将询问挂在树节点上,在进行线段树合并时查询就好了. 代码超级容易写挂的 ...
- Flask项目之手机端租房网站功能测试(完结)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶注册和登录以及用户退出功能 二丶上传头像功能和修改用户名功能测试 三丶发布房源以及实名认证功能测试 四丶网站房屋搜索功能 ...