HDU 1496

Equations

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3188 Accepted Submission(s): 1247


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
 1 //网上说这样的方法就是哈希,但我感觉像是彻彻底底的二分,因为根本没有hash函数 

 2 //巧妙之处在于区间等都对称 

 3 #include <iostream>

 4 #include <cstdio>

 5 #include <cstring>

 6 using namespace std;

 7 

 8 int hash1[1000010];//存储大于零的哈希值

 9 int hash2[1000010];//存储小于零的哈希值

10 

11 int main()

12 {

13      //setbuf(stdout,NULL);//(File *stream,char *buf)设置流使用 buf缓冲区 ,若buf为NULL,则不使用缓冲区 

14      int ans,i,j;

15      int a,b,c,d;//最大值为20w 

16      while(cin>>a>>b>>c>>d)

17      {

18          if((a>=0&&b>=0&&c>=0&&d>=0)||(a<0&&b<0&&c<0&&d<0))//没有这个会超时 

19          {//去他大爷,明明说没有0,不加上0一直wa 

20              cout<<0<<endl;

21             // printf("0\n");

22              continue;

23          }

24          memset(hash1,0,sizeof(hash1));

25          memset(hash2,0,sizeof(hash2));

26          for(i=1;i<=100;i++)

27          {

28              for(j=1;j<=100;j++)

29              {

30                  int temp=a*i*i+b*j*j;

31                  if(temp>=0)//不必加上temp小于10w,因为两个for循环和输入已经限制了 

32                  {

33                      hash1[temp]++;

34                  }

35                  else 

36                     hash2[-temp]++;

37              }

38          }

39          ans=0;

40          for(i=1;i<=100;i++)

41          {

42              for(j=1;j<=100;j++)

43              {

44                  int temp=c*i*i+d*j*j;

45                  if(temp>0)

46                  {

47                      ans+=hash2[temp];//别把数组顺序搞反,因为a*x1^2+b*x2^2 = -(c*x3^2+d*x4^2) 

48                  }

49                  else

50                      ans+=hash1[-temp];

51              }

52          }

53          cout<<(ans<<4)<<endl;//每个数都可以取正负,并不是因为解向量排列 

54      }

55      return 0;

56 }

57 //注意:devc++中不能定义全局变量count,它和库函数中的函数名同名了 

 

 

你可能感兴趣的:(HDU)