D - Eqs解题报告(黄杰)

D - Eqs
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Consider equations having the following form: 
a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=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

题目大意:这个。。就是求满足式子的xi的可能性的数目。
题目链接:http://poj.org/problem?id=1840
方法:hash
思路:我觉得这个题就是hash的入门题,能让你初步理解hash的作用,如果采用最暴力的做法,五重循环。。答案理论上是可以出来的,但是绝对超时,所以我么们可以通过大数组来存储,将等式分为两部分,前三个一部分,后两个一部分,通过记录前部分的值所对应下标的数组赋值1,种数sum在加上后部分的值所对应下标的数组的值,因为两部分的值要相同,所以后部分如果相同,则加上的为1,否则加上的为0(大数组需要先前清0),这里我想说的就是数组开的大小,太大内存超限,太小数组不够存,当然可以取大素数模来减小数组,也可以直接开大数组,不过记得分析。
算法实现:

#include<memory.h>
#include<stdio.h>
#define MAX 25000001
short hash[MAX];
int pow[101];
int main()
{
	int a,b,c,d,e;
	int i,j,k,sum;
	for(i=0;i<101;i++)
		pow[i]=(i-50)*(i-50)*(i-50);
	while(scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)!=EOF)                                                   
	{
		sum=0;
		memset(hash,0,sizeof(hash));
		for(i=0;i<101;i++)
			for(j=0;j<101;j++)
				for(k=0;k<101;k++)
					if(i!=50&&j!=50&&k!=50)
						if(a*pow[i]+b*pow[j]+c*pow[k]<=MAX/2&&a*pow[i]+b*pow[j]+c*pow[k]>=-MAX/2)
//这是判断是否超限,如果不满足,是不可能满足两部分相等的。							hash[12500000+a*pow[i]+b*pow[j]+c*pow[k]]++;
		for(i=0;i<101;i++)
			for(j=0;j<101;j++)
				if(i!=50&&j!=50)
					sum+=hash[12500000-d*pow[i]-e*pow[j]];
		printf("%d\n",sum);				
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值