拉格朗日定理(四平方和问题)(暴力法优化)

问题:
输入一个正整数N,输出一个最小字典序的四个数a,b,c,d使它们的平方和等于N。

优化:减少枚举层数。先将可能的平方和c^2 +d^2 存在hash表里。在求得另两个数的平方和后,检查余数是否是两个数的平方。将四层拆分成两层,可以从n^ 4 降到 n^2

#include 
#include
#include
#include
using namespace std;
int N;
int main()
{
	int a,b,c,d;
	map<int,int>cache;
	cin>>N;
	for(c=0;c*c<=N/2;c++){
		for(d=0;c*c+d*d<=N;d++){
			cache[c*c+d*d]=c;//记录可能出现的平方和 
		}
	}
	
	for(a=0;a*a<=N/4;a++){
		for(b=0;a*a+b*b<=N/2;b++){
			if(cache.find(N-a*a-b*b)!=cache.end()){//如果找不到 返回cache.end()
				c=cache[N-a*a-b*b];
				d=int(sqrt(N-a*a-b*b-c*c));
				cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
				return 0;
			}
		}
	}
	
}

你可能感兴趣的:(算法)