POJ 1840 Eqs(暴力)


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

Source

Romania OI 2002

裸的暴力题,比赛的时候开了5个循环,看时间限制在5秒,以为呀,结果本地都跑不过来。

第一次用short的说。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
short hash[25000001];
int a1,a2,a3,a4,a5;

int main()
{
    while(~scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5))
    {
        int ans=0;
        memset(hash,0,sizeof(hash));
        for(int x1=-50;x1<=50;x1++)
        {
            if(x1==0)
               continue;
            for(int x2=-50;x2<=50;x2++)
            {
                if(x2==0)
                    continue;
                int s=(-1)*(a1*x1*x1*x1+a2*x2*x2*x2);
                if(s<0)
                    s+=25000000;
                hash[s]++;
            }
        }
        for(int x1=-50;x1<=50;x1++)
        {
            if(x1==0)
               continue;
            for(int x2=-50;x2<=50;x2++)
            {
                if(x2==0)
                    continue;
                for(int x3=-50;x3<=50;x3++)
                {
                    if(x3==0)
                        continue;
                    int s=a3*x1*x1*x1+a4*x2*x2*x2+a5*x3*x3*x3;
                    if(s<0)
                        s+=25000000;
                    if(hash[s])
                        ans+=hash[s];
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

POJ 1840 Eqs(暴力),布布扣,bubuko.com

时间: 2024-10-11 00:06:12

POJ 1840 Eqs(暴力)的相关文章

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  这个数并不大  可以开这么大

POJ 1840 Eqs(哈希)

题目地址:POJ 1840 sad...整个比赛期间一直以为是用什么定理或数学公式推导来做..一直没仔细看..结果最后5分钟的时候才看到每个元素的数据范围只是[-50,50]...算了..就算看到了也做不出来..因为会MLE,解决MLE需要把hash数组的定义类型定义成short...这我是不可能想出来的....也没遗憾了.. 这题就是先求前两个for循环,将结果用hash数组存起来.再进行后面三个for循环,如果值为相反数的话,说明和为0.然后记录有多少个. 代码如下: #include <i

POJ 1840.Eqs

Eqs Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1840 Description Consider equations having the following form: a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=0 The coefficients are given integers fr

poj 1840 Eqs (Hash)

/* 这题10^8的暴力可以出答案 但是 慢..... 有个小小的bug 出题人卡int 却没注意short 用short可以不用hash 直接搞 49428K 313MS */ #include<iostream> #include<cstdio> #include<cstring> #define base 12500000 using namespace std; int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,ans; short f[125

POJ 1840 Eqs 二分+map/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,

poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】

Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 6851 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It i

POJ 1840 Eqs Hash + 中途相遇法

把等式分成两拨算用中途相遇法就好了. 不过要注意的是这里不能用map,会超时,要自己手写hash,我重载了[]操作符之后用起来和map差不多,很随意 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <qu

poj 1840 简单hash

http://poj.org/problem?id=1840 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

POJ 2029 DP || 暴力

在大矩形中找一个小矩形 使小矩形包含的*最多 暴力或者DP  水题 暴力: #include "stdio.h" #include "string.h" int main() { int n,m,w,i,s,t,j,k,l,ans,sum,x,y; int map[101][101]; while (scanf("%d",&w)!=EOF) { if(w==0) break; scanf("%d%d",&n,&