poj1840(暴力+hash)

思路:枚举x1,x2,并记录结果出现的次数,然后枚举后三个,并查找在前面枚举是否出现过,若出现过n次,则ans+=n;直接用结果做下标的话,int型存不下,short才可以AC。

后来突发奇想,用hash试一下,看看效率怎样。

数组:49084KB 313ms

hash:764KB  266ms

我去,这不科学,空间变小可以预料,居然还变快了,强大的hash。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<hash_map>
using namespace std;
int first[50*50*50+10];
int ecnt,w[10005],v[10005],nex[10005];

void add(int x)
{
    int t=(x+50*50*50*100)/200,flag=1;
    for(int e=first[t];(~e)&&flag;e=nex[e])
    {
        if(v[e]==x)
        {
            flag=0,w[e]++;
        }
    }
    if(flag)
    {
        w[ecnt]=1;
        v[ecnt]=x;
        nex[ecnt]=first[t];
        first[t]=ecnt++;
    }
}
int getcnt(int x)
{
    if(x>50*50*50*50*2||x<-50*50*50*50*2)return 0;
    int t=(x+50*50*50*100)/200;
    for(int e=first[t];(~e);e=nex[e])
    {
        if(v[e]==x)return w[e];
    }
    return 0;
}
int main()
{
    int a1, a2, a3, a4, a5, sum,ans=0;
    scanf ("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
    memset(first,-1,sizeof first);
    ecnt=0;
    for (int x1 = -50; x1 <= 50; ++x1)
    {
        if (x1 == 0) ++x1;
        for (int x2 = -50; x2 <= 50; ++x2)
        {
            if (x2 == 0) ++x2;
            sum = (a1 * x1 * x1 * x1 + a2 * x2 * x2 * x2) * (-1);
            add(sum);
        }
    }
    for (int x3 = -50; x3 <= 50; ++x3)
    {
        if (x3 == 0) ++x3;
        for (int x4 = -50; x4 <= 50; ++x4)
        {
            if (x4 == 0) ++x4;
            for (int x5 = -50; x5 <= 50; ++x5)
            {
                if (x5 == 0) ++x5;
                sum = (a3 * x3 * x3 * x3 + a4 * x4 * x4 * x4 + a5 * x5 * x5 * x5) ;
                ans +=getcnt(sum);
            }
        }
    }
    printf ("%d\n", ans);
    return 0;
}
未用hash的AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxs = 50 * 50 * 50 * 50 * 4 + 10;
unsigned short cnt[maxs];
int main()
{
    int a1, a2, a3, a4, a5, sum;
    scanf ("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
    for (int x1 = -50; x1 <= 50; ++x1)
    {
    if (x1 == 0) ++x1;
        for (int x2 = -50; x2 <= 50; ++x2)
        {
            if (x2 == 0) ++x2;
            sum = (a1 * x1 * x1 * x1 + a2 * x2 * x2 * x2) * (-1);
            if (sum < 0) ++cnt[sum + maxs];
            else ++cnt[sum];
        }
    }
    int ans = 0;
              for (int x3 = -50; x3 <= 50; ++x3)
{
    if (x3 == 0) ++x3;
        for (int x4 = -50; x4 <= 50; ++x4)
        {
            if (x4 == 0) ++x4;
            for (int x5 = -50; x5 <= 50; ++x5)
            {
                if (x5 == 0) ++x5;
                sum = (a3 * x3 * x3 * x3 + a4 * x4 * x4 * x4 + a5 * x5 * x5 * x5) ;
                if (sum < 0) sum += maxs;
                ans += cnt[sum];
            }
        }
    }
    printf ("%d\n", ans);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值