POJ1840 Eqs(哈希)

Eqs
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 15281 Accepted: 7499

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

解题思路 参考http://user.qzone.qq.com/289065406/blog/1304402322

大致题意:

给出一个5元3次方程,输入其5个系数,求它的解的个数

其中系数 ai∈[-50,50]  自变量xi∈[-50,0)∪(0,50]

 

注意:

  若x1 =a, x2=b ,x3=c ,x4=d,x5=e时,与 x1=b, x2=a ,x3=c ,x4 =d, x5=e 代入方程后都得到值0,那么他们视为不同的解。

 

解题思路:

直观的思路:暴力枚举,O(n^5)

题目Time Limit=5000ms,1ms大约可以执行1000条语句,那么5000ms最多执行500W次

每个变量都有100种可能值,那么暴力枚举,5层循环,就是要执行100^5=100E次,等着TLE吧。。。。

 

要AC这题,就要对方程做一个变形


 

即先枚举x1和x2的组合,把所有出现过的 左值 记录打表,然后再枚举x3 x4 x5的组合得到的 右值,如果某个右值等于已经出现的左值,那么我们就得到了一个解

时间复杂度从 O(n^5)降低到 O(n^2+n^3),大约执行100W次

 

 

我们先定义一个映射数组hash[],初始化为0

对于方程左边,当x1=m  ,  x2= n时得到sum,则把用hash[]记录sum : hash[sum]++,表示sum这个值出现了1次

之所以是记录“次数”,而不是记录“是否已出现”,

是因为我们不能保证函数的映射为 1对1 映射,更多的是存在 多对1映射

例如当 a1=a2时,x1=m  ,  x2= n我们得到了sum,但x1=n  ,  x2= m时我们也会得到sum,但是我们说这两个是不同的解,这就是 多对1 的情况了,如果单纯记录sum是否出现过,则会使得 解的个数 减少。

 

其次,为了使得 搜索sum是否出现 的操作为o(1),我们把sum作为下标,那么hash数组的上界就取决于a1 a2 x1 x2的组合,四个量的极端值均为50

因此上界为 50*50^3+50*50^3=12500000,由于sum也可能为负数,因此我们对hash[]的上界进行扩展,扩展到25000000,当sum<0时,我们令sum+=25000000存储到hash[]

由于数组很大,必须使用全局定义

 

同时由于数组很大,用int定义必然会MLE,因此要用char或者short定义数组,推荐short



#include<stdio.h>
#include<string.h>
short hash[25000000];

int main()
{
    int i,j,k;
    int a[5];
    int sum;
    while(~scanf("%d",&a[0]))
    {
        memset(hash,0,sizeof(hash));
        for(i=1;i<5;i++)
            scanf("%d",&a[i]);

        for(i=-50;i<=50;i++)
        {
            if(!i)
                continue;
            for(j=-50;j<=50;j++)
            {
                if(!j)
                continue;
               sum=a[0]*i*i*i+a[1]*j*j*j;
               if(sum<0) sum+=25000000;
               hash[sum]++;


            }


        }

        int ssum=0;
        int ll;

        for(i=-50;i<=50;i++)
        {
            if(!i)
                continue;
            for(j=-50;j<=50;j++)
            {
                if(!j)
                    continue;
                for(k=-50;k<=50;k++)
                {
                    if(!k)  continue;
                    ll=a[2]*i*i*i+a[3]*j*j*j+a[4]*k*k*k;
                    if(ll<0) ll+=25000000;
                    ssum+=hash[ll];

                }
            }
        }

        printf("%d\n",ssum);
    }

    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值