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. CF1182F Maximum Sine【类欧,扩欧】

    题目链接:洛谷 题目描述:求整数$x\in [a,b]$使得$|2px \ mod \ 2q-q|$最小,如果有多个$x$输出最小的. 数据范围:$1\leq a,b,p,q\leq 10^9$ 第一 ...

  2. Maxim-可自定义的Monkey测试工具(Android)

    Maxim 基于monkey做的二次开发,相比原始monkey,新增如下功能 多种随机测试模式:dfs(深度遍历) mix模式(monkey随机测试+控件识别) troy模式(按照控件选择器进行遍历) ...

  3. 使用axios请求的坑

    配置axios在vue-cli中的使用: 在main.js中配置 import axios from "axios" Vue.config.productionTip = fals ...

  4. java微服务简介与实战

    今年做了一段时间的可见光.ceph存储,后端开发微服务项目,在这记录点东西,也方便大家借鉴查找. springboot的项目实例:https://github.com/ityouknow/spring ...

  5. vue devtools无法使用

    vue devtools无法使用 一.总结 一句话总结: 没显示vue devtools调试工具的原因是用了生产环境的版本或是压缩的vue版本,或是没有勾选:允许访问文件网址 二.vue调试工具Dev ...

  6. Qt之编译MySQL数据库驱动(MSVC)

    Qt之编译MySQL数据库驱动(MSVC) (2013-12-13 20:24:46) 转载▼ 标签: qt mysql qmysql qt编译mysql qt之msvc编译mysql 分类: Qt ...

  7. linux上安装phpstudy

    摘要:安装:wget-chttp://lamp.phpstudy.net/phpstudy.bin chmod+xphpstudy.bin  #权限设置./phpstudy.bin#运行安装用时十到几 ...

  8. Git Command之Code Review

    原文链接 准备 Step 1. Create a team and add a teammate Step 2. Create a repository with some content 应用 Cl ...

  9. yum搭建LAMP环境

    LAMP=Linux+Apache(httpd)+Mysql(mariadb)+PHP Apache HTTP 服务器 2.4 文档:http://httpd.apache.org/docs/2.4/ ...

  10. HAProxy+Keepalived 高可用负载均衡

    转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master backup vip(虚拟IP) 192.16 ...