Equations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7057    Accepted Submission(s): 2858

Problem Description
Consider equations having the following form:

a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.

It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.

Determine how many solutions satisfy the given equation.

 
Input
The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.
 
Output
For each test case, output a single line containing the number of the solutions.
 
Sample Input
1 2 3 -4
1 1 1 1
 
Sample Output
39088
0
以空间换时间。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a,b,c,d;
int res;
int s1[];
int s2[];
int main(){ while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
{
res=;
if((a>&&b>&&c>&&d>)||((a<&&b<&&c<&&d<)))
{
printf("0\n");
continue;
}
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
int k=a*i*i+b*j*j;
if(k>=) s1[k]++;
else s2[-k]++;
} for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
int k=c*i*i+d*j*j;
if(k>) res+=s2[k];
else res+=s1[-k];
} printf("%d\n",res*);
} return ; }

折半枚举

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=;
int a,b,c,d;
int res;
int num[MAXN];
int main(){ while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
{
if(a>&&b>&&c>&&d>||a<&&b<&&c<&&d<)
{
printf("0\n");
continue;
}
int res=;
int cnt=;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
num[cnt++]=a*i*i+b*j*j;
sort(num,num+cnt);
for(int i=;i<=;i++)
for(int j=;j<=;j++)
res+=(upper_bound(num,num+cnt,-(c*i*i+d*j*j))-lower_bound(num,num+cnt,-(c*i*i+d*j*j)));
printf("%d\n",res*);
} return ;
}

HDU1496(巧妙hash)的更多相关文章

  1. 折半枚举+Hash(HDU1496升级版)

    题目链接:N - 方程的解 给定一个四元二次方程: Ax1^2+Bx2^2+Cx3^2+Dx4^2=0 试求−1000≤x1,x2,x3,x4≤1000非零整数解的个数. −10000≤A,B,C,D ...

  2. HDU1496 hash

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. 深入理解HashMap(及hash函数的真正巧妙之处)

    原文地址:http://www.iteye.com/topic/539465 Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多 ...

  4. nginx源码分析之hash的实现

    nginx实现了自己的hash数据结构,正如数据结构中讲述的那样,nginx用开放链表法解决冲突,不过不同的是一旦一个hash表被初始化后就不会被修改,即插入和删除,只进行查询操作,所以nginx通过 ...

  5. HashMap的内部实现机制,Hash是怎样实现的,什么时候ReHash

    1.HashMap的内部实现机制 HashMap是对数据结构中哈希表(Hash Table)的实现,Hash表又叫散列表.Hash表是根据关键码Key来访问其对应的值Value的数据结构,它通过一个映 ...

  6. [知识点]字符串Hash

    1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...

  7. [Data Structure & Algorithm] Hash那点事儿

    哈希表(Hash Table)是一种特殊的数据结构,它最大的特点就是可以快速实现查找.插入和删除.因为它独有的特点,Hash表经常被用来解决大数据问题,也因此被广大的程序员所青睐.为了能够更加灵活地使 ...

  8. Java提高篇——通过分析 JDK 源代码研究 Hash 存储机制

    HashMap 和 HashSet 是 Java Collection Framework 的两个重要成员,其中 HashMap 是 Map 接口的常用实现类,HashSet 是 Set 接口的常用实 ...

  9. HashMap、HashSet源代码分析其 Hash 存储机制

    集合和引用 就像引用类型的数组一样,当我们把 Java 对象放入数组之时,并不是真正的把 Java 对象放入数组中,只是把对象的引用放入数组中,每个数组元素都是一个引用变量. 实际上,HashSet ...

随机推荐

  1. Eclipse 安装(Oxygen版本)

    Eclipse 安装(Oxygen版本) Eclipse 最新版本 Eclipse Neon,这个首次鼓励用户使用 Eclipse Installer 来做安装,这是一种由Eclipse Oomph提 ...

  2. UE-9260使用说明2

    生成镜像 1. U-boot 生成u-boot.bin文件 (1) Makefile ifeq ($(ARCH),arm) CROSS_COMPILE = endif 改动为 ifeq ($(ARCH ...

  3. Arduino关于旋转编码器程序的介绍(Reading Rotary Encoders)--by Markdown

    介绍 旋转或编码器是一个角度測量装置. 他用作精确測量电机的旋转角度或者用来控制控制轮子(能够无限旋转,而电位器只能旋转到特定位置).其中有一些还安装了一个能够在轴上按的button,就像音乐播放器的 ...

  4. 今日BBC

    1.随身英语 Brain training 怎样训练大脑? link 2.地道英语 In good nick 品质好.没有损坏(主要是指古老的东西,比方古董) link 3.今日新闻 -------- ...

  5. Ipython基础功能

    ipython:交互式的python命令行 直接在终端敲命令即可进入 安装:pip install ipython 使用:在终端敲“ipython” 与python解释器的使用方法一致 TAB键自动补 ...

  6. python--员工信息管理系统编译及思路

    员工管理系统,顾名思义,应该具有增删查改功能.拿到需求后,应该按照一定的流程依次编写,最后达到程序的统一和兼容. 系统需求如下: 文件存储格式如下: id,name,age,phone,job 1,A ...

  7. JavaScript2种构造函数创建对象的模式以及继承的实现

    第一种模式: function Person(){ } Person.prototype.say=function(){ alert('hello'); } var person=new Person ...

  8. LeetCode120——Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. windows下route命令详解(转载)

    1.具体功能        该命令用于在本地IP路由表中显示和修改条目.使用不带参数的ROUTE可以显示帮助.            2.语法详解        route [-f] [-p] [co ...

  10. 同一世界服务器架构--Erlang游戏服务器

        Erlang最大的优点是方便,很多基础功能都已经集成到Erlang语言中.之前用C++写服务器的时候,管理TCP连接很繁琐,需要写一大堆代码来实现.底层的框架需要写很多代码实现,这样既浪费时间 ...