hdu 1496

题目分类里说这题是hash,我用深搜加剪枝水过~~不过这道题还真算得上一道好题,思路很多,可以用HASH,题目数据再加强一点就更好了

深搜代码如下:

/*
* hdu1496/linux.c
* Created on: 2011-8-5
* Author : ben
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<math.h>

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

int a[4], ans, x[4];

void dfs(int step) {
int i, temp;
x[step]
= 0;
while (x[step] < 100) {
x[step]
++;
if (step < 2) {
dfs(step
+ 1);
}
else {
temp
= 0;
for (i = 0; i < 3; i++) {
temp
+= a[i] * x[i] * x[i];
}
if (temp <= 0 || temp + a[3] * 10000 > 0) {
continue;
}
x[
3] = sqrt(temp / (-a[3]));
if (x[3] <= 100 && x[3] * x[3] * a[3] + temp == 0) {
ans
++;
}
}
}
}

void work() {
int i, j;
while (scanf("%d%d%d%d", &a[0], &a[1], &a[2], &a[3]) == 4) {
for (i = 0; i < 4; i++) {
for (j = i + 1; j < 4; j++) {
if (a[i] < a[j]) {
a[i]
= a[i] ^ a[j];
a[j]
= a[i] ^ a[j];
a[i]
= a[i] ^ a[j];
}
}
}
if (a[3] > 0 || a[0] < 0) {
puts(
"0");
continue;
}
ans
= 0;
dfs(
0);
printf(
"%d\n", ans * 16);
}
}

HASH方法代码:

/*
* hdu1496/linux.c
* Created on: 2011-8-5
* Author : ben
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<math.h>
void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

int hash[2000010];
void work() {
int a, b, c, d, i, j, count;
while (scanf("%d %d %d %d", &a, &b, &c, &d) != EOF) {
if ((a > 0 && b > 0 && c > 0 && d > 0)
|| (a < 0 && b < 0 && c < 0 && d < 0)) {
puts(
"0");
continue;
}
memset(hash,
0, sizeof(hash));
count
= 0;
for (i = 1; i <= 100; i++) {
for (j = 1; j <= 100; j++) {
hash[a
* i * i + b * j * j + 1000000]++;
}
}
for (i = 1; i <= 100; i++) {
for (j = 1; j <= 100; j++) {
count
+= hash[1000000 - c * i * i - d * j * j];
}
}
printf(
"%d\n", 16 * count);
}
}
posted @ 2011-08-05 15:44  moonbay  阅读(185)  评论(0编辑  收藏  举报