POJ1840--------Eqs

Eqs
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 14101   Accepted: 6932

map水一发;


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

Source Code
Problem: 1840		User: 14110103069
Memory: 1208K		Time: 2000MS
Language: G++		Result: Accepted

    Source Code

    #include <iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<map>
    #include<algorithm>
    #define LL long long
    using namespace std;

    int main()
    {
        LL a,b,x,y,z;
        LL tmp;
        map<int,LL>Q;
        while(~scanf("%lld%lld%lld%lld%lld",&a,&b,&x,&y,&z))
        {
            Q.clear();
            for(int i=-50;i<=50;i++)
            {
                if(i==0)continue;
                for(int j=-50;j<=50;j++)
                {
                    if(j==0) continue;
                    tmp=a*i*i*i+b*j*j*j;
                    Q[-tmp]++;
                }
            }
            LL ans=0;
            for(int i=-50;i<=50;i++)
            {
                if(i==0)continue;
                for(int j=-50;j<=50;j++)
                {
                    if(j==0) continue;
                    for(int k=-50;k<=50;k++)
                    {
                        if(k==0 )continue;
                        tmp=x*i*i*i+y*j*j*j+z*k*k*k;
                        if(Q.count(tmp))
                        {
                            ans+=Q[tmp];
                        }

                    }
                }
            }
            printf("%lld\n",ans);
        }
        return 0;
    }

数组哈希:
Source Code
Problem: 1840		User: 14110103069
Memory: 49112K		Time: 547MS
Language: C++		Result: Accepted

    Source Code

    #include <iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<algorithm>
    using namespace std;

    short hx[25000000];

    int main()
    {
        int a1,a2,a3,a4,a5;
        while(~scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5))
        {
            int g;
            memset(hx,0,sizeof(hx));
            for(int i=-50; i<=50; i++)
            {
                if(!i)
                    continue;
                for(int j=-50; j<=50; j++)
                {
                    if(!j)
                    continue;
                    g=(a1*(i*i*i)+a2*(j*j*j))*(-1);
                    if(g<0)
                        g+=25000000;
                        hx[g]++;
                }
            }
            int ans=0;
            for(int i=-50; i<=50; i++)
            {
                if(!i)
                    continue;
                for(int j=-50; j<=50; j++)
                {
                    if(!j)
                    continue;
                    for(int l=-50; l<=50; l++)
                    {
                        if(!l)
                        continue;
                        g=(a3*(i*i*i)+a4*(j*j*j)+a5*(l*l*l));

                        if(g<0)
                            g+=25000000;
                            ans+=hx[g];
                    }
                }
            }
            printf("%d\n",ans);
        }
        return 0;
    }




你可能感兴趣的:(POJ1840--------Eqs)