题意:给出a1, a2, a3, a4, a5
求满足表达式 a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 的解(x1, x2, x3, x4, x5)的个数,其中xi范围是[-50, 50], xi != 0 (i = 1, 2, 3, 4, 5) 
分析:这题挺简单的,将解分成两组(x1, x2, x3) 和 (x4, x5),先三重循环求出a1x13+ a2x23+ a3x33他们的解,用开散列法对应到数组中,再用两重循环求出a4x43+ a5x53求得结果在hash表中查找然后判断就行了。
代码
#include<stdio.h>
#include
<string.h>
#include
<iostream>
using namespace std;
#define NN 1000020
#define MAXHASH 100003 //这是一个素数
int x1, x2, x3, x4, x5;
int a1, a2, a3, a4, a5;
int idx;

typedef
struct po{
int sum;
struct po *nxt;

}PO;

PO f[NN];
PO
*head[MAXHASH];

int cube(int x){
return x * x * x;
}

int hash(int x){
int h = x % MAXHASH;
if(h < 0) h += MAXHASH;
return h;
}
int OK(int tmp){
int ans = 0;
int h = hash(tmp);
for (PO *p = head[h]; p; p = p->nxt){
if(p->sum == tmp){
ans
++;
}
}
return ans;
}

int main() {
int tmp, h;
scanf(
"%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
idx
= 0;
memset(head,
0, sizeof(head));
for (x1 = -50; x1 <= 50; x1++){
if(x1 == 0) continue;
for (x2 = -50; x2 <= 50; x2++){
if(x2 == 0) continue;
for (x3 = -50; x3 <= 50; x3++){
if(x3 == 0) continue;
tmp
= a1 * cube(x1) + a2 * cube(x2) + a3 * cube(x3);
h
= hash(tmp);
f[idx].sum
= tmp;
f[idx].nxt
= head[h];
head[h]
= f + idx++;
}
}
}
int res = 0;
for (x4 = -50; x4 <= 50; x4++){
if(x4 == 0) continue;
for (x5 = -50; x5 <= 50; x5++){
if(x5 == 0) continue;
tmp
= a4 * cube(x4) + a5 * cube(x5);
tmp
= -tmp;
res
+= OK(tmp);
}
}
printf(
"%d\n", res);
return 0;
}

 

posted on 2010-12-04 20:37  ylfdrib  阅读(400)  评论(0编辑  收藏  举报