Eqs - poj 1840(hash)
2015-08-10 11:02
519 查看
题意:对于方程:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,有xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}. 现在给出a1,a2,a3,a4,a5的值,求出满足上面方程的解有多少个。
思路:hash的应用。暴力枚举的话会达到100^5,明显会超时。所以将方程分成-(a1x13+ a2x23 )和 a3x33+a4x43+ a5x53 两部分,若这两部分相等,则为方程的一个解。
附:
Description
Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.
Input
The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.
Output
The output will contain on the first line the number of the solutions for the given equation.
Sample Input
Sample Output
思路:hash的应用。暴力枚举的话会达到100^5,明显会超时。所以将方程分成-(a1x13+ a2x23 )和 a3x33+a4x43+ a5x53 两部分,若这两部分相等,则为方程的一个解。
#include<stdio.h> #include<stdlib.h> #include<string.h> short hash[25000002]; int main(){ int coff[5],base[101]; int i,j,k; int result=0; for(i=0;i<5;i++){ scanf("%d",&coff[i]); } for(i=-50;i<=50;i++){ int tmp=i*i*i; base[i+50]=tmp; } memset(hash,0,sizeof(hash)); for(i=-50;i<=50;i++){ for(j=-50;j<=50;j++){ if(i!=0&&j!=0){ int tmp=coff[0]*base[i+50]+coff[1]*base[j+50]; hash[tmp+12500000]++; } } } for(i=-50;i<=50;i++){ for( j=-50;j<=50;j++){ for(k=-50;k<=50;k++){ if(i!=0&&j!=0&&k!=0){ int tmp=coff[2]*base[i+50]+coff[3]*base[j+50]+coff[4]*base[k+50]; tmp=0-tmp; if(tmp<12500000&&tmp>=-12500000) result+=hash[tmp+12500000]; } } } } printf("%d\n",result); return 0; }
附:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 14021 | Accepted: 6889 |
Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.
Input
The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.
Output
The output will contain on the first line the number of the solutions for the given equation.
Sample Input
37 29 41 43 47
Sample Output
654
相关文章推荐
- java缓存的使用
- [翻译]写给精明Java开发者的测试技巧
- jQuery基于ajax实现带动画效果无刷新柱状图投票代码
- 关于spring文件上传的配置
- 名字的漂亮度
- 保持业务数据同步
- PreferenceActivity 学习
- 软盘问题
- 五种主要多核并行编程方法分析与比较
- poj 1947(树形背包问题)
- hdu1728 逃离迷宫 【bfs记录转向次数 2种】
- 后台调用js,给同一个事件绑定一个方法
- HDU 3853 LOOPS 可能性dp(水
- Python内置类型——list
- 解决MySQL查询不区分大小写
- Android主线程到底是什么(二)
- WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享
- curl,fsocketopen,socket 三种函数抓取html页面
- apache mvc的运用
- 解决浏览器缓存问题