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. java中并发Queue种类与各自API特点以及使用场景!

    一 先说下队列 队列是一种数据结构.它有两个基本操作:在队列尾部加入一个元素,和从队列头部移除一个元素(注意不要弄混队列的头部和尾部) 就是说,队列以一种先进先出的方式管理数据,如果你试图向一个 已经 ...

  2. Shell-help格式详解

    前言 linux shell命令通常可以通过-h或--help来打印帮助说明,或者通过man命令来查看帮助,有时候我们也会给自己的程序写简单的帮助说明,其实帮助说明格式是有规律可循的 帮助示例 下面是 ...

  3. Shell中三种引号的用法及区别

    Linux Shell中有三种引号,分别为双引号(" ").单引号(' ')以及反引号(` `). 其中双引号对字符串中出现的$.''.`和\进行替换:单引号不进行替换,将字符串中 ...

  4. 用HTML+CSS实现--折叠效果

    下图是一个Accordion组件,请用HTML+CSS实现其UI,并用面向对象的思路把折叠效果JS实现.如果能用纯css的方式实现其折叠效果更佳.PS/这是小米15年的一道校招笔试题,无意间看到就实现 ...

  5. Django基础 - 修改默认SQLite3数据库连接为MySQL

    Django数据库连接默认为SQLite3,打开setting.py可以看到数据库部分的配置如下: DATABASES = { 'default': { 'ENGINE': 'django.db.ba ...

  6. cuowu

    ngFor不能用于Object rowspan colspan不能绑定变量,要用attr.colspan https://stackoverflow.com/questions/35615751/wh ...

  7. Django项目流程

    创建项目和应用 django-admin.py startproject project_name cd project_name python manage.py startapp app_name ...

  8. SSIS 学习之旅 序章 和 简介

    SSIS 学习之旅目录: 第一章: SSIS 学习之旅 第一个SSIS 示例(一) 第二章: SSIS 学习之旅 第一个SSIS 示例(二) 第三章: SSIS 学习之旅 数据同步 第四章: SSIS ...

  9. hdu 3951 硬币围成一圈(博弈)

    n个硬币围成一个环 每次只能取1-K个硬币 最后取完者胜 假如5个硬币 每次取1-2个情况1 先手取1个 后手取剩下4个中间2个 破坏了连续 虽然最后剩2个,但先手只能取一个 然后后再取一个 后手胜 ...

  10. Java第三阶段学习(二、IO流--------递归,字节流Stream)

    一.递归 定义:指方法在方法内调用自己 适用于方法的运算主体不变,但运行的时候,参与运算的方法参数会变化注意:一定要给递归一个出口,否则内存溢出 练习题1:使用递归打印文件夹中所有的文件,包含子目录中 ...