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. django_session

    基于cookie做用户验证时:敏感信息不适合放在cookie中 session依赖cookie session原理 cookie是保存在用户浏览器端的键值对 session是保存在服务器端的键值对 s ...

  2. 百科知识 .tar.xz文件如何打开

    7-ZIP可以打开,右击提取到当前目录即可   发现这个压缩比例还是相当不一般的,都快十倍了.

  3. MySQL 建立外键约束

    http://www.jzxue.com/shujuku/mysql/201109/06-8742.html MySQL 建立外键约束的语法太晦涩难懂了, 不得不记下笔记. 1. 在建表时建立外键 C ...

  4. windows下通过VNC图形化訪问Ubuntu桌面环境

    要在windows下图形化訪问Ubuntu或其他Linux系统桌面环境有非常多方法.我比較喜欢的是使用VNC服务,须要在Ubuntu下安装vncserver和在windows下安装client訪问工具 ...

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

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

  6. Mysql 存储过程使用游标

    -- 完整例子 CREATE PROCEDURE test BEGIN -- 定义参数    DECLARE _id INT; -- 定义游标    DECLARE no_more_products ...

  7. ASP.NET MVC EXTJS 通用主菜单框架

    一.说明 首先我不知道定义的文章标题是不是准确,我这篇博文介绍的是一个通用的软件主菜单框架,界面布局用的是extjs,还是先上一个图吧. 软件主界面左侧菜单采用的风格是extjs的手风琴模式,需要注意 ...

  8. 2018.11.22-day24 面向对象-继承

    1.归一化设计 2.抽象类 3.钻石继承 4.C3算法 5.新式类中的super

  9. 对ShortCut和TWMKey的研究

    TWMKey = packed record Msg: Cardinal; CharCode: Word; Unused: Word; KeyData: Longint; Result: Longin ...

  10. SVN在myeclipse中新建地址的时出现被拒绝

    新建资源库地址: 控制台提示信息: svn: connection refused by the server svn: connection refused by the server Connec ...