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 ...
随机推荐
- mysql 数据库 存储数据类型
int 类型的数据 可以在数据库里存成 char字符串类型的数据: 纯数字的字符串 可以在数据库里存储为 int的数据类型.
- 玩转Bash脚本:选择结构之case
总第5篇 之前,我们谈到了if. 这次我们来谈还有一种选择结构--case. case与if if用于选择的条件,不是非常多的情况,假设选择的条件太多.一系列的if.elif,.也是醉了. 没错,ca ...
- JavaScript中的*top、*left、*width、*Height具体解释
来源:http://www.ido321.com/911.html html代码 1: <body> 2: <div class="father" id=&quo ...
- java三元表达式编程规范问题
package day01; public class Program { public static void main(String[] args) { // TODO Auto-g ...
- 28.semaphore跨进程通信
根据id创建Semaphore,并初始化有一个信号量可用 name类型是char *...; HANDLE hsem = CreateSemaphoreA(NULL, 1, , name); 关闭句柄 ...
- 用硬件卡克隆Linux集群
650) this.width=650;" onclick="window.open("http://blog.51cto.com/viewpic.php?refimg= ...
- C#如何调用非托管的C++Dll
现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前搞VC++开发的人都转向用更强大的VS.Net.在这种情况下,有很多开发人员就面临了如何在C#中使用C++开发好的类 ...
- 百度地图API 添加标签
1.手动创建数据,实际项目则是接受GPS信息 /建立坐标点: // lng:经度 lat:纬度 var points = [ {"lng":112.58,"lat&quo ...
- iOS界面生命周期过程具体解释
开发过Android的人都知道,每个Android界面就是一个Activity,而每个Activity都会有自己的生命周期, 有一系列方法会控制Activity的生命周期.如:onCreate(),o ...
- ORA-01078错误举例:SID的大写和小写错误
案例重演: dbca建库.SID:metro --手工建库时实例名小写的metro ...... [oracle@org54 ~]$ export ORACLE_SID=METRO ...