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);
}
 
 

 












转载于:https://www.cnblogs.com/stxy-ferryman/p/8470188.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值