码迷,mamicode.com
首页 > 其他好文 > 详细

poj1840 Eqs(hash+折半枚举)

时间:2018-02-25 20:47:03      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:algo   思路   sam   rip   hash   sys   pos   stream   val   

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+折半枚举)

标签:algo   思路   sam   rip   hash   sys   pos   stream   val   

原文地址:https://www.cnblogs.com/stxy-ferryman/p/8470188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!