poj 1840 Eqs

Eqs
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 15037   Accepted: 7372

Description

Consider equations having the following form: 
a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=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. 

Input

The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.

Output

The output will contain on the first line the number of the solutions for the given equation.

Sample Input

37 29 41 43 47

Sample Output

654

Source

Romania OI 2002

题意:

求方程a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0解的个数

思路:

先预处理前3项的值,再枚举后两项的值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int mod=10007;
int head[105*105*105],cnt;
struct node
{
    int sum,next;
}E[105*105*105];
void insertHash(int x)
{
    if(x<0) x=-x;
    int h=(x%mod+x/mod)%mod;//哈希函数
    E[cnt].sum=x;
    E[cnt].next=head[h];
    head[h]=cnt++;
}
int  Find(int x)
{
    int num=0;
    if(x<0) x=-x;
    int h=(x%mod+x/mod)%mod;
    for(int i=head[h];i!=-1;i=E[i].next)
    {
        if(E[i].sum==x)
            num++;
    }
    return num;
}
int main()
{
    int x1,x2,x3,x4,x5,a[5];
    while(scanf("%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4])!=EOF)
    {
        cnt=0;memset(E,0,sizeof(E));
        memset(head,-1,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;
                    int temp=x1*x1*x1*a[0]+x2*x2*x2*a[1]+x3*x3*x3*a[2];
                    insertHash(temp);
                }
            }
        }
        int ans=0;
        for(x4=-50;x4<=50;x4++)
        {
            if(x4==0) continue;
            for(x5=-50;x5<=50;x5++)
            {
                if(x5==0) continue;
                int temp=-(a[3]*x4*x4*x4+x5*x5*x5*a[4]);
                int t1=Find(temp);
                ans+=t1;
            }
        }
        cout<<ans/2<<endl;
    }
    return 0;
}



你可能感兴趣的:(poj 1840 Eqs)