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. Count 1 in Binary

    Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, r ...

  2. Android 抽屉导航

    原文地址 http://developer.android.com/training/implementing-navigation/nav-drawer.html 创建抽屉导航 导航抽屉是在 屏幕左 ...

  3. JavaBean的实用工具Lombok(省去get、set等方法)

    转:https://blog.csdn.net/ghsau/article/details/52334762 背景   我们在开发过程中,通常都会定义大量的JavaBean,然后通过IDE去生成其属性 ...

  4. 试用Redis

    Windows 10家庭中文版,运行于VirtualBox上的Ubuntu 18.04,Redis 4.0.10, Redis,久仰大名!因为没有从事互联网行业,所以一直没有使用过.近期找工作,也隐约 ...

  5. spring boot jpa 多数据源配置

    在实际项目中往往会使用2个数据源,这个时候就需要做额外的配置了.下面的配置在2.0.1.RELEASE 测试通过 1.配置文件 配置两个数据源 spring.datasource.url=jdbc:m ...

  6. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  7. fsevents npm install是报错

    npm install 安装插件的时候,fsevents报错,这是node 8.x版本的问题,解决办法,把node 版本切换到6.x

  8. 2017-2018-2 20165301 实验四《Java面向对象程序设计》实验报告

    2017-2018-2 20165301 实验四<Java面向对象程序设计>实验报告 一.Android Stuidio的安装测试 实验要求: 参考<Java和Android开发学习 ...

  9. (一) solr的安装与配置

    载solr文件压缩包,并解压 ,要运行solr服务之前需要先安装jdk,具体安装过程可以参看下面这篇文章: http://www.cnblogs.com/xiazh/archive/2012/05/2 ...

  10. MST最小生成树

    首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...