poj 1840 Eqs(暴力列挙+hash)


テーマリンク
                                                                                                                    Eqs
Time Limit:5000 MS
 
メモリLimit:65536 K
Total Submissions:13405
 
Acceepted:6583
Description
Consinder equations having the following form:
a 1 x 1
3+a 2 x 2
3+a 3 x 3
3+a 4 x 4
3+a 5 x 5
3=0
The coefficients are given integers from the interval[-50,50]
It is consider a solution a system(x 1,x 2,x 3,x 4,x 5)that verifees the equation,xi(-50,50)xi!0,any i∈{1,2,3,4,5}
Determine how many soutions satisfy the given equation.
Input
The only line of input contains the 5 coefficients a 1,a 2,a 3,a 4,a 5,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
問題を二つに分けて列挙する.先にx 1を列挙して、x 2の値、a 1 x 1^3+a 2 x 2^3のすべての値を統計します(値が大きすぎてここでhashを使います).x 3、x 4、x 5の値を列挙して、答えを統計すればいいです.複雑度O(10^6)
コードは以下の通りです
#include
#include
#include
#include
#include
#include
#include
#define inff 0x3fffffff
#define nn 1100000
#define mod 1000003
typedef __int64 LL;
typedef unsigned __int64 LLU;
const LL tem=131313131;
using namespace std;
int a[6];
bool use[nn];
int num[nn];
int ha[nn];
void add(int x)
{
    int ix=(x%mod+mod)%mod;
    for(int i=ix;;i=(i+1)%mod)
    {
        if(!use[i])
        {
            use[i]=true;
            ha[i]=x;
            num[i]++;
            break;
        }
        else if(ha[i]==x)
        {
            num[i]++;
            break;
        }
    }
}
int Hash(int x)
{
    int ix=(x%mod+mod)%mod;
    for(int i=ix;;i=(i+1)%mod)
    {
        if(!use[i])
        {
            return -1;
        }
        else if(ha[i]==x)
            return i;
    }
    return -1;
}
int main()
{
    int i,j,k;
    int ix;
    while(scanf("%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5])!=EOF)
    {
        memset(num,0,sizeof(num));
        memset(use,false,sizeof(use));
        for(i=-50;i<=50;i++)
        {
            if(i==0)
                continue;
            for(j=-50;j<=50;j++)
            {
                if(j==0)
                    continue;
                ix=a[1]*i*i*i+a[2]*j*j*j;
                add(ix);
            }
        }
        LL ans=0;
        int tem;
        for(i=-50;i<=50;i++)
        {
            if(i==0)
                continue;
            for(j=-50;j<=50;j++)
            {
                if(j==0)
                    continue;
                for(k=-50;k<=50;k++)
                {
                    if(k==0)
                        continue;
                    ix=a[3]*i*i*i+a[4]*j*j*j+a[5]*k*k*k;
                    tem=Hash(-ix);
                    if(tem!=-1)
                        ans+=num[tem];
                }
            }
        }
        printf("%I64d
",ans); } return 0; }