HDOJ 1496 Equations hash
2014-02-18 15:05
239 查看
Equations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4745 Accepted Submission(s): 1890
Problem Description
Consider equations having the following form:
a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.
It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.
Determine how many solutions satisfy the given equation.
Input
The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.
Output
For each test case, output a single line containing the number of the solutions.
Sample Input
1 2 3 -4 1 1 1 1
Sample Output
39088 0
Author
LL
Source
“2006校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛
Recommend
LL | We have carefully selected several similar problems for you: 1280 1800 1264 2600 2522
题目大意:
给定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做,涨姿势了……
思路:将等式变形为a*x1^2+b*x2^2= -(c*x3^2+d*x4^2) 先用两重循环列举a,b的所有情况,将等式的左边结果存入hash表。再用两重循环列举c,d的所有情况,看看结果的相反数在不在hash表中。统计输出。
优化:x1^2与x1的正负号无关,x2--x4同理,循环时从1-100循环即可,最后答案乘以16;若abcd均大于或小于0,直接输出0.
HDU ACM的PPT上有两种解法。第一种用了大数组,第二种用了小数组+冲突处理。
由于小数组的初始化时间少,第二种解法更优。
#include <iostream> #include <cstring> using namespace std; int a,b,c,d,pin[101],hash[2000010],i,j,sum; int main() { for (i=1;i<=100;++i) pin[i]=i*i; while (cin>>a>>b>>c>>d){ if ((a>0 && b>0 && c>0 && d>0) || (a<0 && b<0 && c<0 && d<0)){ cout<<"0\n"; continue; } memset(hash,0,sizeof(hash)); sum=0; for (i=1;i<=100;++i) for (j=1;j<=100;++j) hash[a*pin[i]+b*pin[j]+1000000]++; for (i=1;i<=100;++i) for (j=1;j<=100;++j) sum+=hash[1000000-(c*pin[i]+d*pin[j])]; cout<<sum*16<<endl; } return 0; }
#include <iostream> #include <cstring> using namespace std; const int MAX=50021; int f[MAX],g[MAX]; int a,b,c,d,pin[101],i,j,sum,s,p; int hash(int k){ int t=k%MAX; if (t<0) t+=MAX; while (f[t]!=0 && g[t]!=k) t=(t+1)%MAX; return t; } int main() { for (i=1;i<=100;++i) pin[i]=i*i; while (cin>>a>>b>>c>>d){ if ((a>0 && b>0 && c>0 && d>0) || (a<0 && b<0 && c<0 && d<0)){ cout<<"0\n"; continue; } memset(f,0,sizeof(f)); sum=0; for (i=1;i<=100;++i) for (j=1;j<=100;++j){ s=a*pin[i]+b*pin[j]; p=hash(s); g[p]=s; f[p]++; } for (i=1;i<=100;++i) for (j=1;j<=100;++j){ s=-(c*pin[i]+d*pin[j]); p=hash(s); sum+=f[p]; } cout<<(sum<<4)<<endl; } return 0; }
kdwycz的网站: http://kdwycz.com/
kdwyz的刷题空间:http://blog.csdn.net/kdwycz
相关文章推荐
- JAVA线程间的状态转换
- Android rom定制相关
- 30分钟学会jquery插件
- 小知识点
- printStackTrace()、toString()、getMessage()的区别
- Log4j的配置文件介绍
- 万年历
- 根据权限 下载文件
- 《高效学习OpenGL》之模型变换 glTranslatef(), glRotatef(),glScalef()
- c语言注意事项
- CMystring类实现以及返回值的理解
- 命令行编程笔记
- ANDROID 探究oom内幕
- 初入项目,坑自己的习惯,小结
- 编译时和运行时
- MAT 工具使用详解
- 8种网站防止盗链的方法
- Ubuntu系统下使用技巧汇总(持续更新中~)
- 业界netflix的推荐系统
- 协方差的意义