Hdu 1496 Equations (Dual Loop + hash search)

Source: Internet
Author: User

A quartile quadratic equation is used to give the values of a, B, c, d, and xi between [-100,100] but not equal to zero. How many solutions are there when the equation is equal to zero.

Questions can be violent... but they are always not harmonious.

Enumerate the possible values of a * x1 * x1 + B * x2 * x2 and hash them. Then, use hash to search for c * x3 * x3 + d * x4 * x4, multiply the Count value by 16 (xi can be positive or negative)

After a conflict is processed, the hash array can be much smaller, because the dual 100 loop has a maximum of 10000 different answers.

 

# Include <iostream> # include <algorithm> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # include <string> # include <vector> # include <set> # include <queue> # include <stack> # include <climits> // define MAX 50050 # define INF 0x7fffff # define REP (I, s, t) for (int I = (s); I <= (t); ++ I) # define ll long # define mem (a, B) memset (a, B, sizeof (a) # define mp (a, B) make_pa Ir (a, B) # define eps 1e-5 // # pragma comment (linker, "/STACK: 36777216") // The legendary plug-in using namespace std; int hash [MAX], cnt [MAX]; int HASH (int key) // handle conflicts {int t = key % MAX; if (t <0) t + = MAX; while (hash [t]! = Key & cnt [t]! = 0) {t = (t + 1) % MAX;} return t;} int main () {int a, B, c, d; while (scanf ("% d", & a, & B, & c, & d )! = EOF) {mem (hash, 0); mem (cnt, 0); if (a> 0 & B> 0 & c> 0 & d> 0) | (a <0 & B <0 & c <0 & d <0) {printf ("0 \ n"); continue;} REP (I, 1,100) {REP (j, 1,100) {int sum = a * I + B * j; int tmp = HASH (sum); hash [tmp] = sum; cnt [tmp] ++;} int ans = 0; REP (I, 1,100) {REP (j, 1,100) {int sum = c * I + d * j; int tmp = HASH (-sum); ans + = cnt [tmp] ;}} printf ("% d \ n", ans * 16);} return 0 ;}# include <iostrea M> # include <algorithm> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # include <string> # include <vector> # include <set> # include <queue> # include <stack> # include <climits> // # define MAX 50050 # define INF 0x7FFFFFFF # define REP (I, s, t) for (int I = (s); I <= (t); ++ I) # define ll long # define mem (a, B) memset (a, B, sizeof (a) # define mp (a, B) make_pair (a, B) # define eps 1e-5 // # pragma Comment (linker, "/STACK: 36777216") // The legendary plug-in using namespace std; int hash [MAX], cnt [MAX]; int HASH (int key) // handle the conflict {int t = key % MAX; if (t <0) t + = MAX; while (hash [t]! = Key & cnt [t]! = 0) {t = (t + 1) % MAX;} return t;} int main () {int a, B, c, d; while (scanf ("% d", & a, & B, & c, & d )! = EOF) {mem (hash, 0); mem (cnt, 0); if (a> 0 & B> 0 & c> 0 & d> 0) | (a <0 & B <0 & c <0 & d <0) {printf ("0 \ n"); continue;} REP (I, 1,100) {REP (j, 1,100) {int sum = a * I + B * j; int tmp = HASH (sum); hash [tmp] = sum; cnt [tmp] ++;} int ans = 0; REP (I, 1,100) {REP (j, 1,100) {int sum = c * I + d * j; int tmp = HASH (-sum); ans + = cnt [tmp] ;}} printf ("% d \ n", ans * 16);} return 0 ;}

 

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.