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. html2canvas原理

    html2canvas有2种模式,一种是利用foreignObject,一种是纯canvas绘制 1.foreignObject到canvas 步骤: 1.把要截图的dom克隆一份,过程中把getCo ...

  2. Python中greenlet和gevent使用示例

    目录 greenlet示例 示例1,线程切换 示例2 gevent 示例1 示例2: gevent使用monkey对所有系统自带的IO操作打patch 示例3,发送请求 示例4:使用gevent的so ...

  3. Mybatis 返回值 返回Map的为空的值

    第一种.springMVC和boot通用配置:(Mybatis.xml) <?xml version="1.0" encoding="UTF-8"?> ...

  4. centos7 安装 mysql5.6(mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz)

    1.到mysql官网下载安装包 下载地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择以下截图中的版本 2.下载后上传到lin ...

  5. CF1195B

    CF1195B 题意: 有一个盒子,每次可以做两个操作: 1.每次吃掉一块蛋糕 2.每次放入比上一次放入数多1的蛋糕 当盒子为空时,只能执行第 $ 2 $ 个操作.第 $ 1 $ 次操作永远是放入一个 ...

  6. nginx open_file_cache指令影响静态文件更新时间

    有这样的需求,客户上传图片,覆盖以前的图片,因为客户可能会频繁的换图片,上传覆盖完后,页面会自动请求刚才的图片,图片已经换掉,但是url并没有换,因为图片名字没改. 因为更新的图要及时显示所以这个目录 ...

  7. ROS indigo下Kinect v2的驱动安装与调试

    ROS indigo下Kinect v2的驱动安装与调试 一.libfreenect2源码安装与测试 github地址:https://github.com/OpenKinect/libfreenec ...

  8. 通过xshell上传和下载文件

    参考:http://www.cnblogs.com/mingaixin/p/5588699.html

  9. C++ 基础学习笔记

    c++中的左值与右值 左值与右值这两概念是从 c 中传承而来的,在 c 中,左值指的是既能够出现在等号左边也能出现在等号右边的变量(或表达式),右值指的则是只能出现在等号右边的变量(或表达式). 清除 ...

  10. Android-Handler消息机制实现原理)(转)

    Android-Handler消息机制实现原理   一.消息机制流程简介 在应用启动的时候,会执行程序的入口函数main(),main()里面会创建一个Looper对象,然后通过这个Looper对象开 ...