poj 1840 哈希+离散化

点击打开链接

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int M =12500000;
short hash[M*2+10];
//给出coefficients ai 求 a1 X1^3 + a2 X2^3 +  a3X3^3 +  a4X4^3 +  a5 X5^3=0 的解个数   xi∈[-50,50] xi != 0
 // 暴力枚举 5个循环 10^10 肯定TLE 
//    所以变形     a3X3^3 +  a4X4^3 +  a5 X5^3=-(a1 X1^3 + a2 X2^3)
// 枚举右边的和并保存在HASH中 O(n^2)   在暴力枚举左边的和 O(n^3) 看在HAsh中是否存在即可 
int main()
{
	int a1,a2,a3,a4,a5;
	cin>>a1>>a2>>a3>>a4>>a5;
	
	memset(hash,0,sizeof(hash));
	for(int x1=-50;x1<=50;x1++)
	{
		if(!x1) continue;//xi !=0 
		for(int x2=-50;x2<=50;x2++)
		{
			if(!x2) continue;
			int t1=a1*x1*x1*x1;
			int t2=a2*x2*x2*x2;
			long sum=-1*(t1+t2);  //  -M <=sum<=M 
			if(sum<0)
			sum+=M*2; // 下标不为负数 离散化后 负数sum: M<sum<2M 
			
			hash[sum]++; // a+b=sum  x1=a,x2=b 和x1=b,x2=a 视为不同解 
			
		}
	}
	int ans=0;
	for(int x3=-50;x3<=50;x3++)
	{
		if(!x3) continue;
		for(int x4=-50;x4<=50;x4++)
		{
			if(!x4) continue;
			for(int x5=-50;x5<=50;x5++)
			{
				if(!x5) continue;
				long sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
				if(sum<0)
				sum+=M*2;
				
				ans+=hash[sum];
				
				
			}
			
		}
		
	}
	cout<<ans<<endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值