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

时间: 2024-08-08 08:25:15

poj1840 Eqs(hash+折半枚举)的相关文章

Eqs 折半枚举+二分查找 大水题

Eqs 题目抽象:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 (*),给出a1,a2,a3,a4,a5.    ai属于[-50,50]. 求有多少序列   x1,x2,x3,x4,x5 ,xi属于 [-50,50]-{0}. 思路:折半枚举+二分查找 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #inclu

ACDREAM 1726 A Math game(折半枚举+hash)

题目链接: http://acdream.info/problem?pid=1726 题意: 给定n 个数,和一个数看,判断k能否由其中的任意个数的和组成. 分析: 因为n最大为40,暴力枚举所有的情况 复杂度为 2^40 肯定TLE ,然后就想到了折半枚举 分成两半,先处理前n/2个数的组合的情况 ,把所得结果哈希一下,然后再枚举后一半 的所有情况,然后在哈希表里查找.时间复杂度为 O(2^(N/2)); 代码如下: #include <stdio.h> #include <iostr

POJ 1840 Eqs(hash)

题意  输入a1,a2,a3,a4,a5  求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立   a,x取值在-50到50之间 直接暴力的话肯定会超时的   100的五次方  10e了都    然后可以考虑将等式变一下形   把a1*x1^3+a2*x2^3移到右边   也就是-(a1*x1^3+a2^x2^3)=a3*x3^3+a4*x4^3+a5*x5^3 考虑到a1*x1^3+a2^x2^3的最大值50*50^3+50*50^3=12500000  这个数并不大  可以开这么大

Educational Codeforces Round 76 F 折半枚举

Educational Codeforces Round 76 F 折半枚举 https://codeforces.com/contest/1257/problem/F 题意: 数组a,找到一个x使得a中每一个元素异或x后"二进制中1的个数"相同. 数组长度100,数字大小2^30. 思路: 折半枚举答案X,如分为X前15位和后15位.之后我们再枚举期望的"相同个数"是多少,通过hash看看能不能满足就好了. 代码: #include <bits/stdc++

Load Balancing 折半枚举大法好啊

Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <

lightoj 1235 Coin Change (IV)(折半枚举)

话说这是俺们学校暑假集训完的一道题,刚看到以为是到水题,后来发现者复杂度也太大了,受不了了,比赛完也没搞出来,然后欣爷说这是折半枚举.然后就摸摸的学了一下,又把这道题写了一下, 所谓折半枚举就是先算出来一半,再算一半,然后用二分查找看看能不能搞到这一发状态,可以的话就是可以了, 题意:给你两个数n,k,下面再给你n个数,表示你现在有的硬币的面值,每种硬币面值的有两个,看是否可以支付k 题解思路:首先以为只有三种状态,直接dfs就好了,后来发现复杂度太大了,然后死了撸就是上面的,详细情残考代码 源

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

HDU 5340 Three Palindromes( 折半枚举+Manacher+记录区间 )

Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 809    Accepted Submission(s): 240 Problem Description Can we divided a given string S into three nonempty palindromes? Input F

poj 2785 4 Values whose Sum is 0 折半枚举

题目链接:http://poj.org/problem?id=2785 枚举的一般思路就是先把所有的状态枚举出来 最后一次性判断该状态合法不合法 而折半枚举的思想是 先枚举一半的状态 把他们的状态存起来 排序 然后再枚举剩下一般 用目标反推前一半的期望状态 接下来在前一半的结果数组中查找是否有相应结果 之所以能优化是因为结果数组有序 就可以用二分搜索 复杂度从O(n^2 * n^2) 降到 O(n^2 * log(n^2))即(O(n^2 * log n)) 二分搜索的一个技巧 在有序数组中用二