Equations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6065 Accepted Submission(s): 2455
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.
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.
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
这个题目是学习hash应用的第一个题 ,认真研究了好长时间,对比着杭电ACM算法课件。
hash介绍:
哈希表(散列表)的基本原理:
使用一个
下标范围比较大的数组
来存储元素,一般通过设计一个函数(哈希函数,即散列函数),使得每个元素的关键字都与一个函数值(即数组下标)相对应,然后用该数组单元来存储对应元素
冲突:
n
由于不能够保证每个元素的关键字与函数值是一一对应的,因此很有可能出现如下情况:
n
“
对于不同的元素关键字,Hash函数计算出了相同的函数值
”
,这就是产生了所谓的
“
冲突
”
。
n
换句话说,就是Hash函数把不同的元素分在了相同的下标单元。
冲突解决:
方法很多~
常用方法:线性探测再散列技术
即:当 h(k)位置已经存储有元素的时候,依次探查 (h(k)+i) mod S,i=1,2,3…,直到找到空的存储单元为止。其中, S为 数组长度。
特别地,如果将数组扫描一圈仍未发现空单元,则说明哈希表已满,这会带来麻烦,但是,该情况完全可以通过扩大数组范围来避免。
AC:
这里的经验是,对于大数组某些时候可能没有那么多元素,这个时候推荐用动态内存分配。否则提交的时候发现栈溢出。另外动态内存分配后当用到时元素初始值为0,不用
memset函数了,否则 和不用new一个效果,已经把所需内存分配完了。
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int a,b,c,d,ls[101];
for(int i=1;i<=100;i++)
ls[i]=i*i;
while(cin>>a>>b>>c>>d)
{int *hash=new int[2000003];
// memset(hash,0,2000003);
if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
{
cout<<0<<endl;
continue;
}
for(int j=1;j<=100;j++)
for(int t=1;t<=100;t++)
hash[a*ls[j]+b*ls[t]+1000000]++;
int sum=0;
for(int m=1;m<=100;m++)
for(int h=1;h<=100;h++)
{
sum+=hash[-(c*ls[m]+d*ls[h])+1000000];
}
cout<<sum*16<<endl;
delete [] hash;
}
return 0;
}
第二种方法是解决冲突 ,分配较小数组。要注意哈希表不能溢出。另外 如果第一种方法使用动态内存分配,也并不占用多少内存,没必要利用这种方法优化吧。
两个数组 gq【】和ls【】分别表示某一结果出现的次数和把函数值在哈希表中分配合理位置。此过程中可能出现同一个t 对应于不同的值,如果出现不等于上次保留的值
那么改换保留这个值的位置。
#include<stdio.h>
#include<string.h>
const int M=50021;
int ls [M];int gq[M];
int hashh(int k)
{
int t;
t=k%M;
if(t<0)
t+=M;
while(gq[t]!=0&&ls[t]!=k)
{
t=(t+1)%M;
}
return t;
}
int main()
{
int st[101];
int a,b,c,d;
for(int i=1;i<=100;i++)
st[i]=i*i;
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
{
memset(gq,0,sizeof(gq));
if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))
{
printf("%d\n",0);
continue;
}
for(int i=1;i<=100;i++)
for(int j=1;j<=100;j++)
{
int cnt=a*st[i]+b*st[j];
int dic=hashh(cnt);
ls[dic]=cnt;
gq[dic]++;
}
int sum=0;
for(int m=1;m<=100;m++)
for(int g=1;g<=100;g++)
{
int cnt=-(c*st[m]+d*st[g]);
int dic=hashh(cnt);
sum+=gq[dic];
}
printf("%d\n",sum*16);
}
return 0;
}