Hdu 1496 hash

题目链接

对于方程 a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0  ( -50<=a, b, c, d <=50, 且均不为0,-100 <= x1, x2, x3, x4 <= 100, 且均不为0)

在已知a, b, c, d的情况下,计算有多少组x1, x2, x3, x4满足要求.

 

如果枚举x1, x2, x3, 那么理论上10^6可解, 不过我没有尝试...

采用另一种思路, a*x1^2 + b*x2^2 = -c*x3^2 + (-d)*x4^2

等式两边范围均为:-1000000~1000000

附上代码:

 1 /*************************************************************************  2  > File Name: 1496.cpp  3  > Author: Stomach_ache  4  > Mail: [email protected]  5  > Created Time: 2014年05月14日 星期三 16时22分46秒  6  > Propose:  7  ************************************************************************/

 8 

 9 #include <cmath>

10 #include <string>

11 #include <cstdio>

12 #include <vector>

13 #include <fstream>

14 #include <cstring>

15 #include <iostream>

16 #include <algorithm>

17 using namespace std; 18 

19 int a, b, c, d; 20 #define MAX_N (1000000+2)

21 int hash[MAX_N<<1]; 22 //标记每次hash值改变的下标 23 //若每次都用memset(hash, 0, sizeof(hash))会TLE

24 vector<int> flag; 25 

26 int

27 main(void) { 28     memset(hash, 0, sizeof(hash)); 29       while (~scanf("%d %d %d %d", &a, &b, &c, &d)) { 30           c *= -1; 31         d *= -1; 32         for (int i = 1; i <= 100; i++) { 33               for (int j = 1; j <= 100; j++) { 34                   hash[a*i*i+b*j*j+MAX_N] += 4; 35                 flag.push_back(a*i*i+b*j*j+MAX_N); 36  } 37  } 38         int ans = 0; 39         for (int i = 1; i <= 100; i++) { 40               for (int j = 1; j <= 100; j++) { 41                   if (hash[c*i*i+d*j*j+MAX_N] > 0) { 42                       ans += 4 * hash[c*i*i+d*j*j+MAX_N]; 43  } 44  } 45  } 46         for (size_t i = 0; i < flag.size(); i++) { 47               hash[flag[i]] = 0; 48  } 49  flag.clear(); 50         

51         printf("%d\n", ans); 52  } 53 

54     return 0; 55 }

 

你可能感兴趣的:(hash)