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. d3js 添加数据

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  2. Nouveau源代码分析(三):NVIDIA设备初始化之nouveau_drm_probe

    Nouveau源代码分析(三) 向DRM注冊了Nouveau驱动之后,内核中的PCI模块就会扫描全部没有相应驱动的设备,然后和nouveau_drm_pci_table对比. 对于匹配的设备,PCI模 ...

  3. find and xargs

    调整搜索深度 -mandepth 搜索当前目录,而不进入子目录: find . -maxdepth 0 -name "debug*" Linux中find常见用法示例 ·find  ...

  4. [转] git clone 远程分支

    git clone只能clone远程库的master分支,无法clone所有分支,解决办法如下: 找一个干净目录,假设是git_work cd git_work git clone http://my ...

  5. [转]eclipse查看某个java类属于哪个jar包

    原文地址:https://blog.csdn.net/csdnliuxin123524/article/details/73572836 在eclipse界面直接按ctrl+shift+t,弹出以下界 ...

  6. 自动提交form表单

    <form class="form-inline" name='MD5form' method="post" action="<?php ...

  7. 开源G711(PCMA、PCMU)/G726转AAC项目EasyAACEncoder

    EasyDarwin开源社区整理了一份G711(PCMA.PCMU)/G726转AAC的转码库,支持Windows/Linux跨平台使用,将安防标准的G711转成移动互联网常用的AAC格式,希望能给大 ...

  8. 九度OJ 1129:Skew数 (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:734 解决:548 题目描述: 在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1).  每个位上的可能数字是0 ...

  9. 关于java的线程

    1 java的线程也是一个对象 所以,java线程对象也是由gc销毁的. 2 java线程对象等待被销毁的时机 当java线程执行完run()方法之后就在等待被销毁了,所以要一个线程对象不被销毁唯一的 ...

  10. BitMapFactory.decodeFile 总是返回NULL的原因之一

    之前碰到过,顺便记录下来,如果出现这种情况,首先看看是否有对这个文件的读权限.