题意:求x在(-100<=x<=100)区间上,已知a,b,c,d,满足a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 的情况有多少种

思路:很明显四层for循环肯定超时,可用三层for循环来判断,或是,两两组合,求多少种情况。分正负故最后

要乘上2^4;

题目

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

AC代码

哈希:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f1[1000005],f2[1000005];//f1保存得数是正的  f2保存得数是负的
int a,b,c,d;
int main()
{while(~scanf("%d%d%d%d",&a,&b,&c,&d)){if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0)) //abcd全部大于0或者小于0,肯定无解。要加上这个,不然超时{printf("0\n");continue;}memset(f1,0,sizeof(f1));memset(f2,0,sizeof(f2));for(int i=1; i<=100; i++)for(int j=1; j<=100; j++){int ans=a*i*i+b*j*j;if(ans>=0) f1[ans]++;else f2[-ans]++;}int sum=0;for(int i=1; i<=100; i++)for(int j=1; j<=100; j++){int ans=c*i*i+d*j*j;if(ans>0) sum+=f2[ans];else sum+=f1[-ans];}printf("%d\n",16*sum);//每个解有正有负,结果有2^4种}return 0;
}

三层for循环:

#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>0&&b>0&&c>0&&d>0||a<0&&b<0&&c<0&&d<0){printf("0\n");continue;}int sum=0;for(int i=1; i<=100; i++)//因为正负号不同不影响平方的效果,所以只枚举正数for(int j=1; j<=100; j++)for(int l=1; l<=100; l++){int f=a*i*i+b*j*j+c*l*l;if(f%d==0){int h=sqrt(abs((0-f)/d));if(h<=100&&h>0&&h*h==abs((0-f)/d)&&f+d*h*h==0)sum++;}}printf("%d\n",sum*16);}return 0;
}

Equations HDU - 1496(哈希或三层for循环)求满足公式有多少种情况相关推荐

  1. Hdu 1496 Equations

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1496 此题可以用哈希数组求解,至于什么是哈希,可以先看以下这篇推荐文章,写得挺不错的. 推荐 ...

  2. hdu 1496 Equations(技巧hash)

    题意,给你a,b,c,d这4个数的值,然后问a*x1^2 + b*x2^2 +  c*x3^2 + d*x4^2 = 0 的(x1,x2,x3,x4)解一共有多少种?   初看这题,想直接4次循环找, ...

  3. js三层数组循环遍历(笛卡尔积)运算,并生成顺序

    笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尔积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员 [1]  . 假 ...

  4. Hdu 1496 Equations(巧妙哈希)

    题目链接 题目大意:给你一个方程:a*x1^2+b*x2^2+c*x3^2+d*x4^2=0,输入方程的4个系数,求出它的解的个数,其中系数 ai∈[-50,50]  xi∈[-100,0)∪(0,1 ...

  5. hdu 1496 QQpet exploratory park 水概率dp

    题目 犯了个语言的错误: C语言中"%%"会输出一个"%" ,我在本地用1个"%"还是输出了"%". 代码: #incl ...

  6. hdu 1174:爆头(计算几何,三维叉积求点到线的距离)

    爆头 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  7. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  8. 【HDU - 4217 】Data Structure? (线段树求第k小数)

    题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...

  9. HDU 2298 Toxophily 【三分算法 or 直接推导物理公式】

    http://acm.hdu.edu.cn/showproblem.php?pid=2298 题目大意:Bob在(0,0)点想射在(x, y)点的水果,初始的速度为v(已知), g=9.8, 求最小的 ...

最新文章

  1. linux下eclipse cdt引用函数库设置问题
  2. ubuntu禁用锁屏
  3. mount and fstab的使用(整理)
  4. HDU - 4825 Xor Sum(字典树)
  5. server新手引导 sql_Web SQL初体验之新手指导(全功能解析)
  6. Makefile中用宏定义进行条件编译(gcc -D)/在Makefile中进行宏定义-D
  7. Apple System: Error: ENFILE: file table overflow
  8. 远程登录阿里云上的MySQL
  9. 【报告分享】2021四大行业品牌蓝V社媒内容运营观察报告.pdf(附下载链接)
  10. 试题6 算法训练 数据交换(指针)
  11. SparkSql性能测试案例
  12. 终极算法-机器学习和人工智能如何重塑世界:电子书
  13. 【解决方案】施工现场如何实现全方位视频监控?EasyGBS视频智能告警分析平台搭建智慧工地
  14. 英语口译笔记法实战指导 吴钟明 pdf_三口过关,名师指导备考干货帖(内附口译笔记讲解)...
  15. lsd 特征点匹配代码_直线匹配-LSD算法
  16. 江苏2021高考成绩查询全省排名,江苏省高考排名对应大学-江苏考生位次查询(2021年参考)...
  17. Android端百度地图API使用详解
  18. 八皇后时间复杂度_【精神分裂症】首次发病未治疗精神分裂症患者大脑皮质复杂度改变...
  19. 序列化之Serialize
  20. 嵌入式主板的应用领域

热门文章

  1. svn之迁移代码技巧
  2. linux之nautilus .命令浏览当前文件目录
  3. LeetCode之Palindrome Number(回文数)
  4. C++之‘nullptr’ was not declared in this scope
  5. mysql导出二进制日志_使用mysqlbinlog提取二进制日志
  6. php保存附件到指定服务器,如何在PHP中将电子邮件附件保存到服务器?
  7. linux系统做的小游戏,2007最新 100个Linux系统上的小游戏汇集
  8. python123可以复制粘贴吗_使用win32com和python复制和粘贴隔离
  9. 年仅53岁,因连续工作、过度劳累,这位抗疫幕后的科研专家去世
  10. 有图有真相!这世界上,竟有人跟你长得一模一样!