Eqs
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 15133   Accepted: 7426

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

解题思路:

直观的思路:暴力枚举,O(n^5)

题目Time Limit=5000ms,1ms大约可以执行1000条语句,那么5000ms最多执行500W次

每个变量都有100种可能值,那么暴力枚举,5层循环,就是要执行100^5=100E次,等着TLE吧。。。。

要AC这题,就要对方程做一个变形

即先枚举x1和x2的组合,把所有出现过的 左值 记录打表,然后再枚举x3 x4 x5的组合得到的 右值,如果某个右值等于已经出现的左值,那么我们就得到了一个解

时间复杂度从 O(n^5)降低到 O(n^2+n^3),大约执行100W次

我们先定义一个映射数组hash[],初始化为0

对于方程左边,当x1=m  ,  x2= n时得到sum,则把用hash[]记录sum : hash[sum]++,表示sum这个值出现了1次

之所以是记录“次数”,而不是记录“是否已出现”,

是因为我们不能保证函数的映射为 1对1 映射,更多的是存在 多对1映射

例如当 a1=a2时,x1=m  ,  x2= n我们得到了sum,但x1=n  ,  x2= m时我们也会得到sum,但是我们说这两个是不同的解,这就是 多对1 的情况了,如果单纯记录sum是否出现过,则会使得 解的个数 减少。

其次,为了使得 搜索sum是否出现 的操作为o(1),我们把sum作为下标,那么hash数组的上界就取决于a1 a2 x1 x2的组合,四个量的极端值均为50

因此上界为 50*50^3+50*50^3=12500000,由于sum也可能为负数,因此我们对hash[]的上界进行扩展,扩展到25000000,当sum<0时,我们令sum+=25000000存储到hash[]

由于数组很大,必须使用全局定义

同时由于数组很大,用int定义必然会MLE,因此要用char或者short定义数组,推荐short

#include<cstdio>
#include<cstring>
using namespace std;
#define N 25000000
#define t 50
int a1,a2,a3,a4,a5;
short hash[N+];
int main(){
while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)==){
memset(hash,,sizeof hash);
for(int x1=-t;x1<=t;x1++){
if(!x1) continue;
for(int x2=-t;x2<=t;x2++){
if(!x2) continue;
int sum=(a1*x1*x1*x1+a2*x2*x2*x2)*(-);
if(sum<) sum+=N;
hash[sum]++;
}
}
int ans=;
for(int x3=-t;x3<=t;x3++){
if(!x3) continue;
for(int x4=-t;x4<=t;x4++){
if(!x4) continue;
for(int x5=-t;x5<=t;x5++){
if(!x5) continue;
int sum=(a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5);
if(sum<) sum+=N;
if(hash[sum])
ans+=hash[sum];
}
}
}
printf("%d\n",ans);
}
return ;
}

poj1840的更多相关文章

  1. POJ1840 hash

    POJ1840 问题重述: 给定系数a1,a2, ..,a5,求满足a1 * x1 ^ 3 + a2 * x2 ^ 3 +... + a5 * x5 ^ 3 = 0的 xi 的组数.其中ai, xi都 ...

  2. POJ-1840 Eqs---二分

    题目链接: https://vjudge.net/problem/POJ-1840 题目大意: 给出一个5元3次方程,输入其5个系数,求它的解的个数 其中系数 ai∈[-50,50]  自变量xi∈[ ...

  3. poj1840 哈希

    虽然这题目我曾经在我们学校OJ上做过但是我那时候是用的暴力做的,这次我用的是哈希写的,我写这题目时候开始是在main函数里面写哈希感觉很麻烦很不清晰,然后我换用函数来写,清晰了很多,写完就AC了.用哈 ...

  4. POJ1840: Eqs(hash问题)

    一道典型的hash问题: 已知a1,a2,a3,a4,a5,求有多少种不同的<x1,x2,x3,x4,x5>组合满足等式: a1*x1^3 + a2*x2^3 + a3*x3^3 + a4 ...

  5. poj1840 Eqs(hash+折半枚举)

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  6. poj1840 五项式等于0(哈希)

    题目传送门 题意很好懂,注意一下xi不能等于0 思路:智商检测题,一开始想着五重for暴力...Orz,后来移向(把a4a5移到右边)了发现减了1e8数量级的复杂度,再次Orz,所以直接三重循环,记录 ...

  7. POJ1840 Eqs

    题意描述 Eqs 求一个五元方程 \(a_1x_1^3+a_2x_2^3+a_3x_3^3+a_4x_4^3+a_5x_5^3=0\) 的解的个数. 题中给出 \(a_i\) 的值并且保证 \(-50 ...

  8. poj题目

    poj2965 poj1753:标准的BFS+位运算优化 poj1328:线段覆盖变种,把圆对应到线段上,贪心求解 poj2109:高精度开根,二分+高精度,注意要判断答案的位数,如果按照题目给的范围 ...

  9. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

随机推荐

  1. HTML5 Canvas 笛卡尔坐标系转换尝试

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  2. Split Animation Clip From FBX and Multiply Mode Sprite

    Use Script To Creat 2D Animation Clip From Multiply Mode Sprite 很多时候美工拿过来的一张序列帧图片,我们需要转换成 Multiply M ...

  3. Activity设置切换动画时黑屏问题的解决

    //当这么设置的时候.打开Acticity的时候会黑屏一下 overridePendingTransition(R.anim.activity_open,0); //改成例如以下代码 完美解决这个问题 ...

  4. LeetCode——Anagrams

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  5. Spark on Yarn 集群运行要点

    实验版本:spark-1.6.0-bin-hadoop2.6 本次实验主要是想在已有的Hadoop集群上使用Spark,无需过多配置 1.下载&解压到一台使用spark的机器上即可 2.修改配 ...

  6. 光栅化规则(Rasterization Rules)

    光栅化规则不是唯一的,只要能满足在扫描线填充过程中,对于一条分割线两边的像素能够被不重复不遗漏地填充即可. 在gdi3d中目前使用的是下面光栅化规则: xLeft_int=ceil(xLeft-0.5 ...

  7. Java 8中的 Streams API 详解

    为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...

  8. poj 2828 Buy Tickets (线段树 单节点 查询位置更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15533   Accepted: 7759 Desc ...

  9. cookie转coontoin

    /// <summary> /// 一个到多个Cookie的字符串添加到CookieCollection集合中[isGood代码] /// </summary> /// < ...

  10. nginx源码学习_数据结构(ngx_pool_t)

    nginx中关于ngx_pool_t的数据结构位于src/core/ngx_palloc.c和src/core/ngx_palloc.h中,该数据结构主要是和内存池相关的,写下这篇博客前参考了网上很多 ...