hd1496----> Is this question a number theory of water and water?

Source: Internet
Author: User

title:http://acm.hdu.edu.cn/showproblem.php?pid=1496

Test instructions

Consider equations has 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 the 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.

Is the problem very water? However, little did not think so, it may be the small dedication too water!. Although the idea is very simple, but there are two skills worth learning! (Perhaps wit you, already familiar with these two techniques).

Step One: Transform the formula--a*x1^2+b*x2^2=-c*x3^2-d*x4^2 (as if the transformation is very idiotic, as if there is no egg use!) )

The second step: we define a constant shift:-->a*x1^2+b*x2^2+shift=-c*x3^2-d*x4^2+shift. (It seems that there is no more eggs!) ,,,,,,)

Step three: Look directly at the code and you'll understand. Will understand that the problem is simple but really witty!

#include <iostream>#include<cstring>#include<cstdio>using namespacestd;Const intMAXN = 2e6+Ten;Const intShift =1e6;intF[MAXN];intMain () {intA, B, C, D;  while(cin>>a>>b>>c>>d) {intI, J, k, ans =0; if(a<0&&b<0&&c<0&&d<0|| A>0&&b>0&&c>0&&d>0) {cout<<0<<endl;Continue; } memset (F,0,sizeof(f));  for(i=1; i<= -; i++)         for(j=1; j<= -; J + +) F[a*i*i+b*j*j+shift]++;  for(i=1; i<= -; i++)         for(j=1; j<= -; J + +) ans+=f[-c*i*i-d*j*j+Shift]; cout<<ans* -<<Endl; }    return 0;}/*Number theory, transformation formula-->a*x1^2+b*x2^2=-c*x3^2-d*x4^2-->a*x1^2+b*x2^2+shift=-c*x3^2-d*x4^2+shift.     Enumerate x1,x2 with a hash table to record all the values of the left-hand, and then the same method hashes the right-side, the same worth the hash value added to the answer. Because the value may be negative, the array overflows, so add an offset value shift*/  

The code of f[] unexpectedly is a hash table, it seems that the hash table is not all complex oh! This hash table is good for the people ah, but also with good wit. In addition shift with the good wit Oh!

hd1496----> Is this question a number theory of water and water?

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.