Poj 1840 Eqs(哈兮+复杂度)

Eqs
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 14031   Accepted: 6893

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


数据范围有点大不能用5个for来循环,1m中程序可执行1000次,5个for()为10e,而此题目的上限是5000*1000.

对于此种题目一般是对公式做变形。

 ,做一个ha[]表来进行映射,先枚举左边的公式,放进哈兮表,然后在枚举右边看是否在ha【】表里。

时间复杂度为n^3(n^2+n^3).





<pre name="code" class="cpp">#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

short  ha[25000001];//为了避免MLE,用short定义,因为数据范围最大为50^4*2,因为等号右边,有个三项相加的所以至少开到大于他。数组小会有Windows

int main()
{
    int s;
    int a1,a2,a3,a4,a5;
    while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)!=EOF)
    {
        memset(ha,0,sizeof(ha));
        for(int i=-50;i<=50;i++)//一定注意不能让i=0,不可以写成for(i=-50;i<=50&&i!=0;i++)如果i==0,循环便中止
        {
            if(0==i)
                continue;
            for(int j=-50;j<=50;j++)
            {
                if(0==j)
                    continue;
                s=a1*i*i*i+a2*j*j*j;
                s*=(-1);
                if(s<0)
                s+=25000000;
                ha[s]++;
            }
        }
        int ans=0;
        for(int x=-50;x<=50;x++)
        {
            if(0==x)
                continue;
            for(int y=-50;y<=50;y++)
            {
                if(y==0)
                    continue;
                for(int z=-50;z<=50;z++)
                {
                    if(0==z)
                        continue;
                    s=a3*x*x*x+a4*y*y*y+a5*z*z*z;
                    if(s<0)//每次复值都要判断s符号,因为下标不能为-,要不处理且s<0会有Windows
                    s+=25000000;
                    if(ha[s])
                    ans+=ha[s];
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


 

你可能感兴趣的:(时间复杂度,hash,poj)