Eqs
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 18299   Accepted: 8933

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

Solution

一开始以为是meet in the middle搜索.....

然而完全没有那么复杂,甚至还可以用暴力map过??

学习了一波hash表!

其实和建边的邻接表很像,就是把某些值系在某个特定的节点上,一般是定一个不大不小的模数来确定位置。

当然可能有重复,不过这就是hash表嘛!接在一起,查询就很接近$O(1)$了。

主要程序:

加入

void add(int v) {
int x = v > 0 ? v : -v;
x = (x % mod + x / mod) % mod;
Edge[++stot] = Node(v, h[x]);
h[x] = stot;
}

查询

int find(int v) {
int ans = 0;
int x = v > 0 ? v : -v;
x = (x % mod + x / mod) % mod;
for(int i = h[x]; i; i = Edge[i].nex)
if(Edge[i].v == v) ans ++;
return ans;
}

很像邻接表吧~

mod是自己定的,这里定的100007,加入或查询都是按固定的mod方案就能固定位置了

#include<iostream>
#include<cstdio>
#define mod 1000007
using namespace std; struct Node {
int v, nex;
Node() { }
Node(int v, int nex) :
v(v), nex(nex) { }
} Edge[]; int stot, h[];
void add(int v) {
int x = v > ? v : -v;
x = (x % mod + x / mod) % mod;
Edge[++stot] = Node(v, h[x]);
h[x] = stot;
} int find(int v) {
int ans = ;
int x = v > ? v : -v;
x = (x % mod + x / mod) % mod;
for(int i = h[x]; i; i = Edge[i].nex)
if(Edge[i].v == v) ans ++;
return ans;
} int main() {
int a1, a2, a3, a4, a5;
scanf("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
for(int x1 = -; x1 <= ; x1 ++) if(x1)
for(int x2 = -; x2 <= ; x2 ++) if(x2) {
int s = x1 * x1 * x1 * a1 + x2 * x2 * x2 * a2;
add(s);
}
int ans = ;
for(int x3 = -; x3 <= ; x3 ++) if(x3)
for(int x4 = -; x4 <= ; x4 ++) if(x4)
for(int x5 = -; x5 <= ; x5 ++) if(x5) {
int s = x3 * x3 * x3 * a3 + x4 * x4 * x4 * a4 + x5 * x5 * x5 * a5;
ans += find(s);
}
printf("%d", ans);
return ;
}

【POJ】1840:Eqs【哈希表】的更多相关文章

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

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 6851 Description ...

  2. POJ 1840 Eqs 解方程式, 水题 难度:0

    题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...

  3. poj 1840 Eqs (hash)

    题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...

  4. POJ 1840 Eqs

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15010   Accepted: 7366 Description ...

  5. POJ 1840 Eqs 二分+map/hash

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

  6. POJ 1840 Eqs(hash)

    题意  输入a1,a2,a3,a4,a5  求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立   a,x取值在-50到50之间 直接暴力的话肯定会超时的   100的五次方  10e了都 ...

  7. POJ 1840 Eqs 暴力

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

  8. POJ 1840 Eqs(乱搞)题解

    思路:这题好像以前有类似的讲过,我们把等式移一下,变成 -(a1*x1^3 + a2*x2^3)== a3*x3^3 + a4*x4^3 + a5*x5^3,那么我们只要先预处理求出左边的答案,然后再 ...

  9. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  10. POJ 3349 Snowflake Snow Snowflakes (哈希表)

    题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...

随机推荐

  1. input 标签禁止输入

    1.鼠标可以点击输入框,但是不能输入 readonly 例如: <input class="layui-input" readonly > 2.鼠标点击输入框出现禁用图 ...

  2. JSP中page,request,session,application四个域对象区别

    page page指当前页面.只在一个jsp页面里有效 . page里的变量没法从index.jsp传递到test.jsp,只要页面跳转了,它们就不见了. pageContext 如果把变量放到pag ...

  3. 分布式git

    分布式 Git 你现在拥有了一个远程 Git 版本库,能为所有开发者共享代码提供服务,在一个本地工作流程下,你也已经熟悉 了基本 Git 命令.你现在可以学习如何利用 Git 提供的一些分布式工作流程 ...

  4. GMM 模型需不需归一化问题

    工作中遇到的问题:在GMM模型中添加一维scale比较大的特征,需不需要归一化 答案:不需要,推导如下:

  5. Docker - CentOS安装Docker

    如果要在CentOS下安装Docker容器,必须是CentOS 7 (64-bit).CentOS 6.5 (64-bit) 或更高的版本,并要求 CentOS 系统内核高于 3.10. uname ...

  6. jersey 过滤器名称绑定的问题 NameBinding Provider

    查资料也不容易查,这个问题困扰了我两天. 当没有 @Provider 的时候 过滤器不会被执行.

  7. Java - 利用StringEscapeUtils对字符串进行各种转义与反转义

    来自:http://blog.csdn.net/chenleixing/article/details/43456987 --------------------------------------- ...

  8. 一步步教你编写不可维护的 PHP 代码

    译者注:这是一篇很棒文章,使用有趣的叙述方式,从反面讲解了作为一个优秀的 PHP 工程师,有哪些事情是你不能做的.请注意哦,此篇文章罗列的行为,都是你要尽量避免的. 随着失业率越来越高,很多人意识到保 ...

  9. Luogu P2069 【松鼠吃果子】

    推荐一波数组模拟链表的讲解 这道题呢,数组写的话不好删除(因为后面要接过来),自然想到链表 对于一个果子,我们可以维护其前驱和后继,我们不妨记与一个点相邻的上面的点为其前驱,下面的点为其后继 观察到题 ...

  10. 从一道简单的dp题中学到的...

    今天想学点动态规划的知识,于是就看了杭电的课件,数塔问题啊,LCS啊都是比较经典的动规了,然后随便看了看就开始做课后练习题... HDOJ 1421 搬寝室 http://acm.hdu.edu.cn ...