杭电 HDU ACM 1496 Equations

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.

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校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛

这个题目是学习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;
}
时间: 2024-08-02 02:49:31

杭电 HDU ACM 1496 Equations的相关文章

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many

杭电 HDU ACM 1025 Constructing Roads In JGShining&#39;s Kingdom

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17732    Accepted Submission(s): 5023 Problem Description JGShining's kingdom consists of 2n(n is no mo

杭电HDU ACM Uncle Tom&#39;s Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496    Accepted Submission(s): 1028 Special Judge Problem Description Your old uncle Tom inherited a piece of land f

杭电 HDU ACM 圆桌会议

圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3356    Accepted Submission(s): 2351 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有

杭电 HDU ACM 1046 Tempter of the Bone

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 83458    Accepted Submission(s): 22740 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

杭电 HDU ACM 1283 最简单的计算机

最简单的计算机 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5238    Accepted Submission(s): 2967 Problem Description 一个名叫是PigHeadThree的研究组织设计了一台实验用的计算机,命名为PpMm.PpMm只能执行简单的六种命令A,B,C,D,E,F:只有二个内存M1,M

杭电 HDU ACM 1213 How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17049    Accepted Submission(s): 8349 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn

杭电 HDU ACM 1407 测试你是否和LTC水平一样高

测试你是否和LTC水平一样高 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14276    Accepted Submission(s): 4701 Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= n