解方程整数解的题,通过这道题我学会了这种题的一般做法,对于未知数较少、取值范围较小且解为整数的方程,把一半的未知数移到等式的另一边,然后对两边分别枚举,用哈希配对,如果有相同的结果就找到一组解。具体做法是先把一边的结果插入hash表,再把另一边的结果在hash表里查找。

Eqs

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 9
Problem 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
PKU
 
 
 #include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <cmath>
#define seed 999983
#define inf 0x7fffffff
using namespace std; struct Hash
{
int val,next;
}h[]; int cnt;
void Insert(int n)
{
int key=abs(n)%seed;
// cout<<key<<endl;
if(h[key].val==inf) //插入的位置没有值
h[key].val=n;
else //插入的位置已有值,冲突处理
{
int i,last;
for(i=key;i!=-;i=h[i].next)
last=i;
i=last;
for(;h[i].val!=inf;i=(i+)%);
h[i].val=n;
h[last].next=i;
}
} void check(int n)
{
int key=abs(n)%seed;
if(h[key].val==inf)
return;
else
{
int i;
for(i=key;i!=-;i=h[i].next)
if(h[i].val==n)
{
// cout<<n<<' '<<h[i].val<<' '<<h[i].next<<endl;
cnt++;
}
return;
}
} int main()
{
// freopen("in.txt","r",stdin);
int a1,a2,a3,a4,a5;
cnt=;
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
int i,j,k;
memset(h,-,sizeof(h));
for(i=;i<;i++)
h[i].val=inf;
for(i=-;i<=;i++)
{
if(i==) //注意未知数都不能为零
continue;
for(j=-;j<=;j++)
{
if(j==)
continue;
int tmp=-(a1*i*i*i+a2*j*j*j);
// cout<<tmp<<endl;
Insert(tmp);
}
}
for(i=-;i<=;i++)
{
if(i==)
continue;
for(j=-;j<=;j++)
{
if(j==)
continue;
for(k=-;k<=;k++)
{
if(k==)
continue;
int tmp=a3*i*i*i+a4*j*j*j+a5*k*k*k;
check(tmp);
}
}
}
printf("%d\n",cnt);
return ;
}

三部曲一(数据结构)-1024-Eqs的更多相关文章

  1. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  2. 三部曲一(数据结构)-1020-Ultra-QuickSort

    通过这道题我大体理解了树状数组的原理和用法,完全用的别人的算法,我把别人算法看懂之后有自己敲了一遍,不得不说这算法真是高深巧妙啊,我开始看都看不懂,还是在别人的讲解下才看懂的,我觉得有必要写个博客记录 ...

  3. 三部曲一(数据结构)-1011-Sorting It All Out

    每次加入一个关系都要进行拓扑排序,不过在排序过程中需要判断是否出现多个入度为0的点,如果出现了就说明不能确定大小关系.不论出不出现多个入度为0的点拓扑排序都要进行到最后来判断是否出现环,因为一旦出现环 ...

  4. linux内核数据结构之kfifo

    1.前言 最近项目中用到一个环形缓冲区(ring buffer),代码是由linux内核的kfifo改过来的.缓冲区在文件系统中经常用到,通过缓冲区缓解cpu读写内存和读写磁盘的速度.例如一个进程A产 ...

  5. 浅谈算法和数据结构: 十 平衡查找树之B树

    前面讲解了平衡查找树中的2-3树以及其实现红黑树.2-3树种,一个节点最多有2个key,而红黑树则使用染色的方式来标识这两个key. 维基百科对B树的定义为“在计算机科学中,B树(B-tree)是一种 ...

  6. Redis 5种数据结构使用及注意事项

    1优缺点 非常非常的快,有测评说比Memcached还快(当大家都是单CPU的时候),而且是无短板的快,读写都一般的快,所有API都差不多快,也没有MySQL Cluster.MongoDB那样更新同 ...

  7. redis 源码阅读 内部数据结构--字符串

    redis的内部数据结构主要有:字符串,双端链表,字典,跳跃表. 这里主要记录redise字符串的设计.相关的源码位于:src/sds.h 和 src/sds.c.   一 字符串 sds的结构体 s ...

  8. 转 浅谈算法和数据结构: 十 平衡查找树之B树

    前面讲解了平衡查找树中的2-3树以及其实现红黑树.2-3树种,一个节点最多有2个key,而红黑树则使用染色的方式来标识这两个key. 维基百科对B树的定义为"在计算机科学中,B树(B-tre ...

  9. 标准BT.656并行数据结构

    转自网络,感谢原作者和转载者. 还有参考:百科http://baike.baidu.com/link?url=bqBT3S7pz_mRJoQE7zkE0K-R1RgQ6FmHNOZ0EjhlSAN_o ...

随机推荐

  1. 模板引擎:ArtTemplate 使用入门和简单的使用

    下载地址:https://github.com/aui/artTemplate 快速上手请参考:https://github.com/aui/artTemplate 通过阅读artTemplate原文 ...

  2. Unix网络编程--卷二:FAQ

    1.编译unpipc库. 执行./configure时报错: checking host system type... Invalid configuration `x86_64-pc-linux-g ...

  3. 【iOS】Foundation框架 学习笔记

    1.数组 OC数组不能存放nil值OC数组只能存放OC对象.不能存放非OC对象类型,比如int.struct.enum等 ====================================== ...

  4. spark配置

    -Dspark.master=local -Xms128m -Xmx512m -XX:MaxPermSize=300m -ea

  5. iOS Error

    1),'libxml/tree.h' file not found Solution: 1.  导入libxml2.dylib 包 2.设置Header Search Paths 为 /usr/inc ...

  6. 【leetcode❤python】 414. Third Maximum Number

    #-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...

  7. Trasformation中的公式报错误,错误数据行定位

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. 剑指offer一:二维数组中的查找

    题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路: 这是一个顺序二维 ...

  9. centos7 禁止防火墙

    #CentOS .0默认使用的是firewall作为防火墙,这里改为iptables防火墙. #firewall: systemctl start firewalld.service#启动firewa ...

  10. JQuery的概述

    一.什么是 jQuery 1.jQuery是一个JavaScript库,jQuery 极大地简化了 JavaScript 编程.它通过封装原生的JavaScript函数得到一整套定义好的方法. 2.它 ...