Hangzhou Electric HDU ACM 1496 equations

Source: Internet
Author: User

Equations Time limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6065 Accepted Submission (s): 2455


Problem Descriptionconsider equations having the following form:

A*x1^2+b*x2^2+c*x3^2+d*x4^2=0
A, B, C, D is integers from the interval [ -50,50] and any of them cannot is 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.

Inputthe input consists of several test cases. Each test case consists of a containing the 4 coefficients a, B, C, D, separated by one or more blanks.
End of file.
Outputfor each test case, output a single line containing the number of the solutions.

Sample Input
1 2 3-41 1 1 1

Sample Output
390880

Authorll
Source "2006 Campus cultural activity Month" of "School Anniversary Cup" College students Program Design Competition and Hangzhou University of Technology fourth session of college students Program Design Contest This topic is the first problem of learning hash application, carefully studied for a long time, compared to hang electric ACM algorithm courseware. Description of the hash:

The basic principle of a hash table (hash table):

  Use aarray with a larger subscript range thanto store elements, typically by designing a function (a hash function, that is, a hash function), so that each element's keyword corresponds to a function value (that is, an array subscript), and then uses that array cell to store the corresponding element Conflict:Nsince it is not guaranteed that the keyword of each element corresponds to the value of the function one by one, there is a good chance that the following will happen:N"for different element keywords, the hash function calculates the same function value", and that is what has produced the so-called"Conflict". Nin other words, the hash function points different elements to the same subscript unit. Conflict Resolution:

A lot of ways ~

Common methods: linear detection and re-hashing technology

that is: when the H (k) position has been stored with elements, then the profiler (H (k) +i) mod s,i=1,2,3 ... until an empty storage unit is found. where S is an array length .

in particular, if the array is scanned in a lap and no empty cells are found, then the hash table is full, which can cause trouble, but it is entirely possible to avoid it by enlarging the array range.

AC:The experience here is that for large arrays there may not be many elements at some point, and dynamic memory allocation is recommended at this time. Otherwise, the stack overflow is found when committed. In addition, when the dynamic memory allocation is used, the initial value of the element is 0.memset function, otherwise, and without a new effect, has allocated the required memory.
#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;}

The second method is to resolve the conflict and allocate a smaller array. Be aware that the hash table cannot overflow. In addition, if the first method uses dynamic memory allocation and does not occupy much memory, it is not necessary to use this method to optimize it.

Two arrays GQ "" and LS "respectively indicate the number of occurrences of a result and assign the value of the function to a reasonable position in the hash table. This procedure may occur when the same t corresponds to a different value if a value that does not equal the last hold is present

Then change the position where this value is retained.

#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;}


Hangzhou Electric HDU ACM 1496 equations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.