This article is from svitter's blog
--Angry grass hash table!
Question
Consider equations having the following form: a1x13 + a2x23 + a3x33 + a4x43 + a5x53 = 0 the coefficients are given integers from the interval [-50,50]. it is consider a solution a system (x1, x2, X3, X4, X5) that verifies the equation, xiε [-50, 50], Xi! = 0, any I ε {1, 2, 3, 4, 5 }.
Determine how many solutions satisfy the given equation.
A1 ~ A5, X1 ~ X5 is in [-50 ~ 50] value, X1 ~ If X5 is not 0, calculate the total number of solutions.
Input/Output Analysis
Write it here and record it. If the space is dynamically opened up, you do not need to use while (~ Scanf) This form may cause MLE or recycle errors.
Sample Input37 29 41 43 47Sample Output654
Algorithm Data Structure Analysis
Calculate the value of I ** 3 and store it in the table, because it must be traversed for countless times. To simplify the calculation, set the 0 point to the position where the coordinate is 50.
Calculate the sum of the first two groups of products and store them in the hash table. Then, take the opposite number for the three groups of values after calculation to check whether they are equal.
Note that do not immediately break the number, because it can be obtained by multiple groups of different combinations, so it is an answer.
AC code
// Author: svtter // # include <iostream> # include <stdio. h> # include <string. h> # include <vector> # include <map> # include <algorithm> # include <queue> # include <cmath> # define INF 0 xffffff # define LLn long # ifdef online_judge # define foi (file) 0 # define fow (File) 0 # else # define foi (File) freopen (file, "r", stdin); # define fow (File) freopen (file, "W", stdout); # endifusing namespace STD; struct hashnode {int V; hashno De * Next;}; int A [5]; int hash [101]; // The previous value is 3533 # define M 3533int I, j, k; int res; hashnode * hash [M + 5]; void buildlist () {for (I =-50; I <0; I ++) {hash [I + 50] = I * I; hash [50-i] =-Hash [I + 50] ;}} void buildhash () {int hashkey; hashnode * TMP; for (I = 0; I <= 100; I ++) {if (I = 50) continue; For (j = 0; j <= 100; j ++) {If (j = 50) continue; Res = A [0] * hash [I] + A [1] * hash [J]; hashkey = (RES> 0? Res:-res) % m; TMP = new hashnode; TMP-> V = res; TMP-> next = hash [hashkey]; hash [hashkey] = TMP ;}}} int main () {// foi ("input"); // fow ("output"); // write your programme here buildlist (); int ans = 0; int hashkey; hashnode * pH; scanf ("% d", & A [0], & A [1], & A [2], & A [3], & A [4]); ans = 0; buildhash (); for (I = 0; I <= 100; I ++) {if (I = 50) continue; For (j = 0; j <= 100; j ++) {If (j = 50) con Tinue; For (k = 0; k <= 100; k ++) {If (k = 50) continue; res = A [2] * hash [I] + A [3] * hash [J] + A [4] * hash [k]; Res =-res; hashkey = (RES> 0? Res:-res) % m; pH = hash [hashkey]; // while (pH! = NULL) {// multiple groups of data may exist if (pH-> V = res) ans ++; pH = Ph-> next ;}}}} printf ("% d \ n", ANS); Return 0 ;}