poj1840 Eqs(hash+折半枚举)

Posted Styx-ferryman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1840 Eqs(hash+折半枚举)相关的知识,希望对你有一定的参考价值。

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
题意:求a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 在x∈[-50,50]且x!=0的解的个数
x1=a且x2=b与x1=b且x2=a算两个解
题解:因为a1,a2,a3,a4,a5是固定的,所以只需要枚举x1,x2,x3,x4,x5即可
复杂度为O(n^5)
等等!O(n^5)?!
这是要t的节奏啊
该怎么办呢?
改下公式吧~
a3x33+ a4x43+ a5x53=-a1x13 -a2x23
这样先枚举右边的解数,再枚举x3,x4,x5,看看满不满足右边即可
这种折半枚举的思路很好,至于如何检验满不满足,本来是准备用map的,结果t了
于是只好hash了……
最好打的hash704ms,好像也不坏
至于poj的abs……emmm也是醉了
代码如下:
#pragma GCC optimize(2)
#include<map>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;

vector<long long> g[100010];
int a1,a2,a3,a4,a5,ans;

int main()
{
    scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
    for(int i=-50; i<=50; i++)
    {
        if(!i)
        {
            continue;
        }
        for(int j=-50; j<=50; j++)
        {
            if(!j)
            {
                continue;
            }
            long long x=a1*(i*i*i)+a2*(j*j*j);
            int key=x<0?(-x)%99991:x%99991;
            g[key].push_back(x);
        }
    }
    for(int i=-50; i<=50; i++)
    {
        if(!i)
        {
            continue;
        }
        for(int j=-50; j<=50; j++)
        {
            if(!j)
            {
                continue;
            }
            for(int k=-50; k<=50; k++)
            {
                if(!k)
                {
                    continue;
                }
                long long y=a3*(i*i*i)+a4*(j*j*j)+a5*(k*k*k);
                int key=y<0?(-y)%99991:y%99991;
                for(int w=0;w<g[key].size();w++)
                {
                    if(g[key][w]==-y)
                    {
                        ans++;
                    }
                }
            }
        }
    }
    printf("%d\n",ans);
}

 












以上是关于poj1840 Eqs(hash+折半枚举)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1840 Eqs

机试练习09:poj1840——Eqs

POJ 1840.Eqs

POJ 1840:Eqs

POJ 1840 -- Eqs

poj3977 折半枚举