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

题目大意:

       给你a,b,c,d这4个数的值,然后问a*x1^2 + b*x2^2 +  c*x3^2 + d*x4^2 = 0
的(x1,x2,x3,x4)解一共有多少种?
思路:
        要想直接暴力4层for循环是不可能的,但可以直接3层暴力;因为正负号不同不影响平方的效果,所以只枚举正数,最后乘以2的4次方(16),表示正负不同的组合:
操作代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
int a,b,c,d;
while(~scanf("%d%d%d%d",&a,&b,&c,&d))
{
if(a>&&b>&&c>&&d>||a<&&b<&&c<&&d<)
{
printf("0\n");
continue;
}
int sum=;
for(int i=; i<=; i++)
for(int j=; j<=; j++)
for(int l=; l<=; l++)
{
int f=a*i*i+b*j*j+c*l*l;
if(f%d==)
{
int h=sqrt(abs((-f)/d));
if(h<=&&h>&&h*h==abs((-f)/d)&&f+d*h*h==)
sum++;
}
}
printf("%d\n",sum*);
}
return ;
}

另外一种可用哈希进行求解,分别把前两个数所算的总和:正数和负数放入两个不同的数组中,之后与后两个的负数与正数进行匹配看最终有多少种?结果还要  *16 哦!

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxx 1001001
int negative[maxx];//记录负数
int positive[maxx];//记录正数
int main()
{
int a,b,c,d;
while(~scanf("%d%d%d%d",&a,&b,&c,&d))
{
if(a>&&b>&&c>&&d>||a<&&b<&&c<&&d<)
{//abcd全部大于0或者小于0,肯定无解。要加上这个,不然超时
printf("0\n");
continue;
}
memset(negative,,sizeof(negative));
memset(positive,,sizeof(positive));
for(int i=; i<=; i++)
for(int j=; j<=; j++)
{
int k=a*i*i+b*j*j;
if(k<=)
negative[-k]++;//k<=0 负值[k]++
else
positive[k]++;//k>0 正值++
}
int sum=;
for(int i=; i<=; i++)
for(int j=; j<=; j++)
{
int k=c*i*i+d*j*j;
if(k<)
sum+=positive[-k];//若k为负,加上正值
else
sum+=negative[k]; //若k为正,加上负值
}
printf("%d\n",*sum); //每个解有正有负,结果有2^4种
}
return ;
}

Equations HDU - 1496(哈希的应用)的更多相关文章

  1. hdu 1496 Equations hash表

    hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...

  2. HDU 1496 Equations(哈希表)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1496 [题目大意] 给出一个方程ax1^2+bx2^2+cx3^2+dx4^2=0,求-100到1 ...

  3. hdu 1496 Equations

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 Equations Description Consider equations having ...

  4. HDU 1496 Equations hash HDU上排名第一!

    看题传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1496 题目大意: 给定a,b,c,d.a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 ...

  5. Equations(hdu 1496 二分查找+各种剪枝)

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

  6. HDU 1496 Equations 等式(二分+暴力,技巧)

    题意:给出4个数字a,b,c,d,求出满足算式a*x1^2+b*x2^2+c*x3^2+d*x4^2=0的 (x1,x2,x3,x4) 的组合数.x的范围[-100,100],四个数字的范围 [-50 ...

  7. HDU - 1496 Equations (hash)

    题意: 多组测试数据. 每组数据有一个方程 a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0,方程中四个未知数 x1, x2, x3, x4 ∈ [-100, 100], 且 ...

  8. HDU 1496

    题目出处:HDU OJ 1496 http://acm.hdu.edu.cn/showproblem.php?pid=1496 为了练习Hash,特定采用了杭电自带的分类列表http://acm.hd ...

  9. HDU 1496 Train Problem I 火车问题1(桟,水)

    题意: 给出两个串,串中的数字i 代表编号为i的火车进入车站的顺序,车站如桟一样,先进后出.第二个串是火车出站的顺序,问若按照第一个串那样进站,是否有可能如第二个串一样的出站顺序?火车顶多9辆,即1- ...

随机推荐

  1. SSH 远程上传本地文件至服务器

    使用SSH命令行传输文件到远程服务器   以前一直在windows下用SSH Secure Shell连接远程服务器,它自带了一个可视化的文件传输工具,跟ftp差不多 但是它也存在一个缺陷,不支持编码 ...

  2. nodejs基础 用http模块 搭建一个简单的web服务器 响应纯文本

    首先说一下,我们平时在浏览器上访问网页,所看到的内容,其实是web服务器传过来的,比如我们访问www.baidu.com.当我们在浏览器地址栏输入之后,浏览器会发送请求到web服务器,然后web服务器 ...

  3. Matlab下imwrite,Uint16的深度图像

    Matlab下imwrite,Uint16的深度图像 1. 在Matlab命令窗口输入命令: help imwrite 会有如下解释: If the input array is of class u ...

  4. 拆分项目搞成framework 实例

    目前工作中遇到的问题,是讲项目三大模块拆分, 将Discover.Shop和Events的源代码拆分到独立的项目中 拆分是个麻烦事,里面相互依赖很多 ,打包编译也异常复杂,各种报错 编译Event 遇 ...

  5. Mac os 安装 alipay-sdk-python 3.3.92错误 line 278,其实是另一个依赖包pycrypto安装有问题。

    日期2019.7.17解决的问题. 系统mac os 10.14.5 python 3.6 django 1.11 要安装alipay-sdk-python 3.3.92错误 line 278, in ...

  6. android: Android 权限管理小结

    一. 概述 感谢郭神,自从Android6.0发布以来,在权限上做出了很大的变动,不再是之前的只要在manifest设置就可以任意获取权限,而是更加的注重用户的隐私和体验,不会再强迫用户因拒绝不该拥有 ...

  7. android java.util.Date和java.util.sql中Date的区别

    1.将java.util.Date 转换为 java.sql.Date java.sql.Date sd; java.util.Date ud; //initialize the ud such as ...

  8. Qt自定义类添加qvector报错

    PtsData& PtsData::operator=(const PtsData& obj){ return *this;} PtsData::~PtsData(){ }

  9. 使用linux flock文件锁实现任务锁定避免计划任务程序冲突

    格式:flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command选项-s, --shared: 获得一个共享锁 -x, --exclusi ...

  10. Linux学习笔记之系统中的分区和文件系统

    转自 http://blog.csdn.net/hanxuehen/article/details/8229472