HDU ACM 1496 Equations
Equations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3978 Accepted Submission(s): 1602
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.
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.
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
Author
LL
Recommend
LL
解题思路:暴力+hash,主要是想练练hash,一直认为hash是高端黑,用线性探测再散列处理冲突,记得不知道谁说过处理余数的数一般用上素数,不要忘了最后乘上16,因为四个数有可能是正负
1 #include<stdio.h> 2 #include<string.h> 3 #define MAXN 50001 4 int num[MAXN], store[MAXN]; 5 6 int hash(int cur) 7 { 8 int temp = cur%MAXN; 9 if(temp < 0) temp += MAXN; 10 while(num[temp] != 0 && store[temp] != cur) 11 { 12 temp = (temp+1)%MAXN; 13 } 14 return temp; 15 } 16 17 int main() 18 { 19 // freopen("input.txt", "r", stdin); 20 int rate[101], a, b, c, d, i, j, sum, temp, res; 21 for(i=0; i<101; ++i) rate[i] = i*i; 22 while(scanf("%d%d%d%d", &a, &b, &c, &d) != EOF) 23 { 24 if((a>0&&b>0&&c>0&&d>0) || (a<0&&b<0&&c<0&&d<0)) 25 { 26 printf("0\n"); 27 continue; 28 } 29 memset(num, 0, sizeof(num)); 30 for(i=1; i<101; ++i) 31 for(j=1; j<101; ++j) 32 { 33 temp = a*rate[i]+b*rate[j]; 34 res = hash(temp); 35 store[res] = temp; 36 num[res]++; 37 } 38 39 sum = 0; 40 41 for(i=1; i<101; ++i) 42 for(j=1; j<101; ++j) 43 { 44 temp = -(c*rate[i]+d*rate[j]); 45 res = hash(temp); 46 sum += num[res]; 47 } 48 49 printf("%d\n", sum*2*2*2*2); 50 } 51 return 0; 52 }

更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
作者:雪影蓝枫
本文版权归作者和博客园共有,欢迎转载,未经作者同意须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?