POJ 1840 Eps 解題報告(哈希)

    a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,xi∈[-50,50],且xi!=0。讓咱們求全部解的可能。優化

    首先,若是暴力判斷的話,每一個x的取值有100種可能,100^5確定會超時。spa

    咱們能夠枚舉x1,x2的值,而且記錄下來。再枚舉x3,x4,x5的值。若是發現有互爲相反數的,說明有一個解存在。複雜度卻大大下降了。code

    固然,咱們能夠只處理正數的狀況。若是存在一組解,x1,x2,x3,x4,x5,那麼容易證實-x1,-x2,-x3,-x4,-x5也是一組解。blog

    咱們只記錄a1x13+ a2x23>0的狀況,若是發現有相等的a3x33+ a4x43+ a5x53,解的數量應該增長2個。string

    通過優化,筆者的代碼在POJ上 94MS 完成。代碼以下:io

#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

unsigned char mp[12500001];

int main()
{
//    freopen("in.txt","r",stdin);
    int a,b,c,d,e;
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);

    int calThird[101];
    for(int i=-50;i<=50;i++)
        calThird[i+50]=i*i*i;

    int zero=0;
    for(int i=-50;i<=50;i++) if(i)
        for(int k=-50;k<=50;k++) if(k)
        {
            int res=a*calThird[i+50]+b*calThird[k+50];
            if(res==0)
                zero++;
            else if(res>0)
            {
                mp[res]+=2;
            }
        }

    int count=0;
    for(int i=-50;i<=50;i++) if(i)
        for(int k=-50;k<=50;k++) if(k)
            for(int j=-50;j<=50;j++) if(j)
        {
            int res=c*calThird[i+50]+d*calThird[k+50]+e*calThird[j+50];
            if(res==0)
                count+=zero;
            else if(res>0 && res<=12500000)
                count+=mp[res];
        }

    printf("%d\n",count);
}
相關文章
相關標籤/搜索