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

  1. 37 29 41 43 47

Sample Output

  1. 654

Source

Romania OI 2002

Solution

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

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

学习了一波hash表!

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

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

主要程序:

加入

  1. void add(int v) {
  2. int x = v > 0 ? v : -v;
  3. x = (x % mod + x / mod) % mod;
  4. Edge[++stot] = Node(v, h[x]);
  5. h[x] = stot;
  6. }

查询

  1. int find(int v) {
  2. int ans = 0;
  3. int x = v > 0 ? v : -v;
  4. x = (x % mod + x / mod) % mod;
  5. for(int i = h[x]; i; i = Edge[i].nex)
  6. if(Edge[i].v == v) ans ++;
  7. return ans;
  8. }

很像邻接表吧~

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #define mod 1000007
  4. using namespace std;
  5.  
  6. struct Node {
  7. int v, nex;
  8. Node() { }
  9. Node(int v, int nex) :
  10. v(v), nex(nex) { }
  11. } Edge[];
  12.  
  13. int stot, h[];
  14. void add(int v) {
  15. int x = v > ? v : -v;
  16. x = (x % mod + x / mod) % mod;
  17. Edge[++stot] = Node(v, h[x]);
  18. h[x] = stot;
  19. }
  20.  
  21. int find(int v) {
  22. int ans = ;
  23. int x = v > ? v : -v;
  24. x = (x % mod + x / mod) % mod;
  25. for(int i = h[x]; i; i = Edge[i].nex)
  26. if(Edge[i].v == v) ans ++;
  27. return ans;
  28. }
  29.  
  30. int main() {
  31. int a1, a2, a3, a4, a5;
  32. scanf("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
  33. for(int x1 = -; x1 <= ; x1 ++) if(x1)
  34. for(int x2 = -; x2 <= ; x2 ++) if(x2) {
  35. int s = x1 * x1 * x1 * a1 + x2 * x2 * x2 * a2;
  36. add(s);
  37. }
  38. int ans = ;
  39. for(int x3 = -; x3 <= ; x3 ++) if(x3)
  40. for(int x4 = -; x4 <= ; x4 ++) if(x4)
  41. for(int x5 = -; x5 <= ; x5 ++) if(x5) {
  42. int s = x3 * x3 * x3 * a3 + x4 * x4 * x4 * a4 + x5 * x5 * x5 * a5;
  43. ans += find(s);
  44. }
  45. printf("%d", ans);
  46. return ;
  47. }

【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. JavaScript入门--慕课网学习笔记

     JAVASCRIPT—(慕课网)入门篇 我们来看看如何写入JS代码?你只需一步操作,使用<script>标签在HTML网页中插入JavaScript代码.注意, <script&g ...

  2. 【算法学习】Fhq-Treap(无旋Treap)

    Treap——大名鼎鼎的随机二叉查找树,以优异的性能和简单的实现在OIer们中广泛流传. 这篇blog介绍一种不需要旋转操作来维护的Treap,即无旋Treap,也称Fhq-Treap. 它的巧妙之处 ...

  3. gpio口、内核定时器使用

    /*申请gpio*/ int gpio_request(unsigned gpio, const char *label); /*设置gpio为输入状态,即设置如(GPH0CON)*/ int gpi ...

  4. Runtime - 消息发送原理

    Runtime - 消息发送原理. Objective-C运行时的核心就在于消息分派器objc_msgSend,消息分派器把选择器映射为函数指针,并调用被引用的函数. 要想理解objc_msgSend ...

  5. Python模块:Random(未完待续)

    本文基于Python 3.6.5的官文random编写. random模块简介 random为各种数学分布算法(distributions)实现了伪随机数生成器. 对于整数,是从一个范围中均匀选择(u ...

  6. NLP里面好的学习资料

    别人推荐的网址: http://ruder.io/deep-learning-nlp-best-practices/index.html#wordembeddings

  7. js面试题之求数组最值

    今天继续分享js常见的面试题,求数组最大值,最小值,这里列举4种常见解法,还有其他方法也可以实现,读者知道可以私信我,我将把意见列举到博客中,欢迎提出意见. 第一种,利用数组排序 var arr=[3 ...

  8. js函数前加分号和感叹号的作用

    js函数前加分号和感叹号是什么意思?有什么用? 一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuery); 今天看到bootstrap的javascrip ...

  9. Java将字符串转成二进制码

    Java将字符串转成二进制码 public void toBinary(){ String str = "王雪"; char[] strChar=str.toCharArray() ...

  10. DDD领域模型企业级系统(一)

    领域模型的基本构造块: 1.实体(Entity):有业务生命周期,使用标识进行跟踪. 2.值对象(Value Object):无业务生命周期,用来描述实体. 3.服务(Service):无状态的行为类 ...