POJ 1840 ——Eqs 哈希,枚举

6 篇文章 0 订阅
4 篇文章 0 订阅
Eqs
Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 11976Accepted: 5850

Description

Consider equations having the following form: 
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=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


这个题如果单纯用暴力枚举的话,必然会超时的,既然这样,我们就把上式钱两项看做一组,后三项看做一组,前两项暴力出来的结果用哈希表存储,后三项暴力枚举出来的结果如果是前两项结果的相反数,那么统计变量就加哈希数组里面的值,如此,O(n^5)就变为了O(n^2+n^3);


#include <stdio.h>
#include <string.h>

short zs[12500010];//经过推算,前两项整数最大是12500000,负数也是,用short类型能不爆内存
short fs[12500010];



int main()
{
    int i,j,k,a,b,c,d,e,sum1,sum2,js=0;
    memset(zs,0,sizeof(zs));
    memset(fs,0,sizeof(fs));
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
    for(i=-50; i<=50; i++)//枚举前两项的值,负数和正数的哈希分情况储存
    {
        for(j=-50; j<=50; j++)
        {
            if(j==0||i==0)
                continue;
            sum1=-a*i*i*i-b*j*j*j;
            if(sum1<0)
                fs[-sum1]++;
            else
                zs[sum1]++;
        }
    }
    for(i=-50; i<=50; i++)//枚举后三项,如果相反数和前两项相等,计数变量加哈希数组里面的值
    {
        for(j=-50; j<=50; j++)
        {
            for(k=-50; k<=50; k++)
            {
                if(k==0||i==0||j==0)
                    continue;
                sum2=c*i*i*i+d*j*j*j+e*k*k*k;
                if(sum2>=-12500010&&sum2<=12500010)
                {
                    if(sum2<=0)
                    {
                        if(zs[-sum2]!=0)
                        {
                            js+=zs[-sum2];
                        }
                    }
                    else
                    {
                        if(fs[sum2]>0)
                        {
                            js+=fs[sum2];
                        }

                    }
                }
            }
        }
    }
    printf("%d\n",js);
    return 0;
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值