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. SolidEdge 工程图中如何标注尺寸公差

    1 先标注基准框,输入基准符号(A B C之类的)   2 点击特征控制,设置马上要标注的特征和公差(可以保存为模板)   3 直接点击要标注的元素即可   4 没有基准的形位公差标注也一样  

  2. C++错误——”无法启动程序”…\xxx.exe” 系统找不到指定的文件”

    背景 在VS2010环境下,升级了一个用C++编写的程序,编译生成成功,但是,不能在VS中直接运行(可以通过打开debug文件目录的exe应用程序运行),提示以下错误:”无法启动程序”…\xxx.ex ...

  3. VS2010/12多核编译

    在工作中,我们的一个完整的项目肯定是由多个个解决方案组成的,我们在调试的时候就会不断的去编译修改过的解决方案,如果当修改的解决方案多了以后我们编译的速度就在很大的程度上决定了我们的工作效率.这时候我们 ...

  4. linux系列之-—04 自动删除n天前日志【转】

    让Linux系统定时清理一些不需要的文件,日志很有必要 1. 删除文件命令: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; ...

  5. Topcoder SRM625 题解

    给出一个字符串求是palindrome和anagram的比率是多少. 知识点: 1 DBL_MAX 64位double的最长数大概是1.7E308,非常大非常大,比long long大上不知多少倍.故 ...

  6. Python实战之自己主动化评论

    Python实战之自己主动化评论 玩csdn博客一个多月了,渐渐发现了一些有意思的事,常常会有人用相同的评论到处刷.不知道是为了加没什么用的积分,还是纯粹为了表达楼主好人.那么问题来了,这种无聊的事情 ...

  7. angular 关于 factory、service、provider的相关用法

    1.factory() Angular里面创建service最简单的方式是使用factory()方法. factory()让我们通过返回一个包含service方法和数据的对象来定义一个service. ...

  8. caffe2--Install

    Install Welcome to Caffe2! Get started with deep learning today by following the step by step guide ...

  9. caffe搭建--ubuntu标准平台的搭建

    http://caffe.berkeleyvision.org/install_apt.html Ubuntu Installation General dependencies sudo apt-g ...

  10. PE添加Style

       1. <style id="NumberStyle"> <setting> <param name="option"> ...