蓝桥四平方和-枚举||二分||哈希

四平方和(蓝桥杯C/C++B组真题详解)(三种做法) 四平方和(蓝桥杯C/C++B组真题详解)蓝桥杯真题 阅读详情

原题描述

四平方和,点击进入原题
参考资料acwing y总
四平方和定理,又称为拉格朗日定理:

每个正整数都可以表示为至多 4 个正整数的平方和。

如果把 0 包括进去,就正好可以表示为 4 个数的平方和。

比如:

5 = 0 2 + 0 2 + 1 2 + 2 2 5 = 0 5 = 0^2 + 0^2 + 1^2 + 2^25=0 5=02+02+12+225=0

7 = 1 2 + 1 2 + 1 2 + 2 2 7 = 1 7 = 1^2 + 1^2 + 1^2 + 2^27=1 7=12+12+12+227=1

对于一个给定的正整数,可能存在多种平方和的表示法。

要求你对 4 个数排序:

0 ≤ a ≤ b ≤ c ≤ d 0 \leq a \leq b \leq c \leq d 0≤a≤b≤c≤d
并对所有的可能表示法按 a , b , c , d a,b,c,d a,b,c,d为联合主键升序排列,最后输出第一个表示法。

枚举(最佳)

我特意在枚举法的后面加了最佳二字,意在告诉自己这道题其实没有想象中那么的复杂。利用3个for循环,将数枚举出来即可

#include <iostream>
#include <cmath>
using namespace std;
int main() {
	int n;
	cin >> n;
	
	for (int a = 0; a*a <= n; a++)
		for (int b = a; a*a+b*b <= n; b++)
			for (int c = b; a*a+b*b+c*c <= n; c++) {
				int t = n - a*a-b*b-c*c;
				int d = sqrt(t); // int对小数会直接向下取整
				if (d*d == t) { // 因此写法成立
					printf("%d %d %d %d\n", a, b, c, d);
					return 0;
				}
			}
	
	return 0;
}

二分

先预处理出c和d的值,并按大小提前进行排列
然后再枚举a、b的过程中,只需判断是否能查找到n-a*a-b*b的值即可
因为c、d的值具有单调,非常明显可以采用二分

// 二分查找
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 2e6 + 10;
int n, m;

struct Num {
	int cd, c, d;
	
	bool operator<(const Num& w) const {
		if (cd != w.cd) return cd < w.cd;
		if (c != w.c) return c < w.c;
		return d < w.d;
	}
}num[N];

int main() {
	cin >> n;
	
	for (int c = 0; c*c <= n; c++) {
		for (int d = c; c*c+d*d <= n; d++) {
			num[m++] = {c*c+d*d, c, d}; // 下标从0开始,区别于哈希的地方
		}
	}
	
	sort(num, num + m);
	
	for (int a = 0; a*a <= n; a++) {
		for (int b = a; a*a + b*b <= n; b++) {
			int t = n - a*a - b*b;
			int l = 0, r = m-1; // 边界为cd的个数
			while(l < r) {
				int mid = l+r>>1;
				if (num[mid].cd >= t) r = mid;
				else l = mid+1;
			}
			if (num[l].cd == t) {
				printf("%d %d %d %d\n", a, b, num[l].c, num[l].d);
				return 0;
			}
		}
	}
	return 0;
}

哈希

由二分查找的基础上推广而来,意在用哈希表来代替二分中的结构体

// 哈希
#include <iostream>
#include <unordered_map>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
int n;
unordered_map<int, PII> m;

int main() {
	cin >> n;
	
	for (int c = 0; c*c <= n; c++) {
		for (int d = c; c*c+d*d <= n; d++) {
			int t = c*c + d*d;
			if (!m.count(t)) m[t] = {c, d}; // 未能找到再插入
		}
	}
	
	for (int a = 0; a*a <= n; a++) {
		for (int b = a; a*a + b*b <= n; b++) {
			int t = n - a*a - b*b;
			if (m.count(t)) {
				printf("%d %d %d %d\n", a, b, m[t].x, m[t].y);
				return 0;
			}
		}
	}
	
	return 0;
}
蓝桥杯/ACM 四平方和(优化枚举算法) 四平方和四平方和定理,又称为拉格朗日定理:每个正整数都可以表示为至多4个正整数的平方和。如果把0包括进去,就正好可以表示为4个数的平方和。比如:5 = 0^2 + 0^2 + 1^2 + 2^27 = 1^2 + 1^2 + 1^2 + 2^2(^符号表示乘方的意思)对于一个给定的正整数,可能存在多种平方和的表示法。要求你对4个数排序:0 &lt;= a &lt;= b &lt;= c &lt;=... 阅读详情

相关推荐

汽车工程师必看:从CAN到Ethernet,6种车载通信协议全解析(附应用场景对比)

本文全面解析了从CAN到Ethernet的6种车载通信协议,包括CAN、LIN、MOST、FlexRay等,并对比了它们的应用场景和技术特点。文章详细介绍了各协议的演进历程、技术优势及在现代汽车中的混合网络架构设计,为汽车工程师提供了实用的协议选型指南和未来通信技术发展趋势。

weixin_28683689的博客 490

蓝桥学院ACM入门基础--第一节 枚举+优化套路(1)#平方十位数--蓝桥杯2017国赛题#STL-set用法#数据类型long long

- 第一节 枚举+优化套路(1)- 枚举的基本思想: 枚举算法的基本思想非常朴素:就是利用计算机强大的运算能力,一个一个试,把答案试出来。 举个例子:我们要求1到100万里有多少个质数。这个答案我没办法用公式或者什么办法直接算出来,只好一个一个试1是不是质数、2是不是质数、3是不是质数……一直试到100万。这就是枚举 枚举的要点: 如果我一个算法根据数据范围、时限我知道要超时,我们怎么减少...

夏普通 934

局域网tcp通信实验

IPv4 地址 . . . . . . . . . . . . : 192.168.92.79。IPv4 地址 . . . . . . . . . . . . : 192.168.92.59。子网掩码 . . . . . . . . . . . . : 255.255.255.0。子网掩码 . . . . . . . . . . . . : 255.255.255.0。使用这篇文章的小工具。

weixin_51883798的博客 431

四平方和 JAVA题解

四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多44个正整数的平方和。 如果把00包括进去,就正好可以表示为44个数的平方和。 比如: 5=02+02+12+225=02+02+12+22 7=12+12+12+227=12+12+12+22 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对44个数排序: 0≤a≤b≤c≤d0≤a≤b≤c≤d...

weixin_41988889的博客 890

盘点那些好用的SAP FIORI App(二) -上传凭证Upload Journal Entry

SAP FICO运维问题及解决方案分享

lijinbao171022的博客 1542

LVGL移植教程(正点原子)

学习正点原子LVGL移植教程,根据视频教程写的博客

papalila的博客 939

[蓝桥杯]四平方和

四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多44个正整数的平方和。 如果把00包括进去,就正好可以表示为44个数的平方和。 比如: 5=02+02+12+225=02+02+12+22 7=12+12+12+227=12+12+12+22 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对44个数排序: 0≤a≤b≤c≤d0≤a≤b≤c≤d 并对所有的可能表示法按a,b,c,da,b,c,d为联合主键升序排列,最后输出第一个表示法。 输入格式...

Stillboring的博客 390

Xilinx IP库在Modelsim/Questasim中的高效仿真实践

本文详细介绍了在Modelsim/Questasim中高效仿真Xilinx FPGA设计的核心方法。针对仿真时常见的IP核缺失错误,重点解析了两种主流解决方案:使用Vivado的compile_simlib命令自动编译完整仿真库,以及利用export_simulation命令为特定工程生成仿真脚本。文章还提供了手动配置库的深入理解、实战排错步骤及高级避坑指南,帮助用户构建稳定、高效的独立仿真环境。

sony5的博客 1229

蓝桥杯四平方和问题

蓝桥杯四平方和问题 四平方和 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和。 如果把0包括进去,就正好可以表示为4个数的平方和。 比如: 5 = 0^2 + 0^2 + 1^2 + 2^2 7 = 1^2 + 1^2 + 1^2 + 2^2 (^符号表示乘方的意思) 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对4个数排序: 0 <= a <= b <= c <= d 并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输

qq_46169330的博客 452

基于STM32的智能血氧、心率、体温、血压监测系统

摘要:本文设计了一款基于STM32F103C8T6的多功能健康监测系统,可实时检测血氧、心率、体温和血压四项生理参数。系统集成MAX30102、DS18B20等传感器模块,采用OLED显示和蓝牙无线传输技术,实现一键血压测量、阈值报警及手机APP数据管理功能。硬件设计包含传感器采集、电源管理、显示报警等模块;软件采用模块化架构,实现数据采集处理、蓝牙通讯等功能。测试表明系统测量准确可靠,为家庭健康监测提供了便携解决方案。未来可扩展更多监测参数和智能分析功能。

3254968421的博客 766

把核心变干净的代价与回报:为什么推动 Clean Core,几乎绕不开 SAP Integration Suite 与 API Management

Clean Core的核心价值在于构建可持续升级的ERP系统架构,其关键不在于减少代码或限制扩展,而是通过解耦和治理实现系统边界的清晰划分。这一理念将推动企业从传统的"私搭乱建"式集成向平台化工程转型,而SAP Integration Suite和API Management正是实现这一转型的核心组件。 Integration Suite作为集成中枢,通过统一平台、可复用内容库和事件驱动架构,将碎片化连接转变为标准化能力;API Management则负责建立接口治理体系,通过统一入口、

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 241

四平方和(蓝桥杯-暴力)

四平方和定理,又称为拉格朗日定理:每个正整数都可以表示为至多4个正整数的平方和。如果把0包括进去,就正好可以表示为4个数的平方和。比如:5 = 0^2 + 0^2 + 1^2 + 2^2    7 = 1^2 + 1^2 + 1^2 + 2^2   (^符号表示乘方的意思)对于一个给定的正整数,可存在多种平方和的表示法。要求你对4个数排序:0 &lt;= a &lt;= b &lt;= c &lt...

Z_sea的博客 788

YOLO26改进策略【Neck】| CVPR 2024 MFM(Modulation Fusion Module,调制融合模块):动态特征加权融合,突出关键特征抑制冗余

本文主要利用DCMPNet中的 MFM 模块优化 YOLO26 的目标检测网络模型。模块通过动态调制特征融合过程,实现了对多尺度、跨层级特征的智能聚合。将其应用于的改进过程中,针对目标检测中边界特征与语义信息的互补性需求,缓解网络中浅层细节与深层语义融合不足的问题。Depth Information Assisted Collaborative Mutual Promotion Network for Single Image Dehazing在图像去雾网络中,不同层级和类型的特征包含着互补的信息(如浅层的

Limiiiing的博客 221

2016蓝桥杯C++A:四平方和(详细讲解)

八、题目:四平方和 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和。 如果把0包括进去,就正好可以表示为4个数的平方和。 比如: 5 = 0^ 2 + 0^ 2 + 1^ 2 + 2^2 7 = 1^ 2 + 1^ 2 + 1^ 2 + 2^2 (^符号表示乘方的意思) 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对4个数排序: 0 <= a ...

gl620321的博客 2338

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

二分/哈希-蓝桥杯省赛-四平方和

二分-蓝桥杯省赛-四平方和 题目: 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多 4 个正整数的平方和。 如果把 0 包括进去,就正好可以表示为 4 个数的平方和。 比如: 5=02+02+12+22 7=12+12+12+22 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对 4 个数排序: 0≤a≤b≤c≤d 并对所有的可能表示法按 a,b,c,d 为联合主键升序...

njuptACMcxk的博客 413

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

蓝桥杯——四平方和

原题链接:四平方和 题目描述 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多 444 个正整数的平方和。 如果把 000 包括进去,就正好可以表示为 444 个数的平方和。 比如: 5=02+02+12+225 = 0^2 + 0^2 + 1^2 + 2^25=02+02+12+22 7=12+12+12+227 = 1^2 + 1^2 + 1^2 + 2^27=12+12+12+22 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对 4 个数排序: 0≤a≤b≤c≤d0≤a≤b

Archerrrrr的博客 354

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

蓝桥杯2016——四平方和

【题目】 四平方和定理,又称为拉格朗日定理:每个正整数都可以表示为至多4个正整数的平方和。如果把0包括进去,就正好可以表示为4个数的平方和。 比如: 5 = 0^2 + 0^2 + 1^2 + 2^2 7 = 1^2 + 1^2 + 1^2 + 2^2 (^符号表示乘方的意思) 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对4个数排序:0 <= a <= b <= c <= d。 并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法 程序输入

wuyu_1016的博客 347

[第七届蓝桥杯省赛C++A/B组]四平方和

来源: 第七届蓝桥杯省赛C++A/B组 算法标签:二分,哈希 题目描述 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多 4 个正整数的平方和。 如果把 0 包括进去,就正好可以表示为 4 个数的平方和。 比如: 5=02+02+12+22 7=12+12+12+22 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对 4 个数排序: 0≤a≤b≤c≤d 并对所有的可能表示...

代码中枢:软件工程理论与实践的桥梁 641

蓝桥杯四平方和定理算法解析与优化

四平方和定理是数论中的经典问题,指出任何正整数都能表示为不超过四个整数的平方和。在算法竞赛如蓝桥杯中,这类问题考察选手的数学建模与算法优化能力。通过暴力枚举、循环优化到哈希表预计算等方法,可以逐步提升解题效率。哈希表法利用空间换时间,将时间复杂度从O(n²)优化到O(n),适合处理大规模数据。这类算法不仅在竞赛中有应用,在密码学、图形学等领域也有实用价值。本文以蓝桥杯真题为例,详细讲解如何运用三重循环优化和哈希表技术解决四平方和问题,并分析各算法的时间复杂度与适用场景。

weixin_30225755的博客 348

蓝桥杯 四平方和【枚举】【哈希表】

四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多 4个正整数的平方和。 如果把 0包括进去,就正好可以表示为 4个数的平方和。 比如: 5=02+02+12+22 7=12+12+12+22 对于一个给定的正整数,可能存在多种平方和的表示法。 要求你对 4个数排序: 0≤a≤b≤c≤d 并对所有的可能表示法按 a,b,c,d为联合主键升序排列,最后输出第一个表示法。 输入格式 输入一个正整数 N。 输出格式 输出4个非负整数,按从小到大排序,中间用空格分开。 数据范围 0<N<5∗

cutlery1137的博客 445

【蓝桥杯】四平方和

蓝桥杯 不支持 c++11 所以unordered_xx 和auto 不能使用。输入一个正整数 N。

m0_53063391的博客 380

四平方和题解(二分习题)

四平方和题解(二分习题)

Bruce 481

蓝桥杯小白打卡第四天

四平方和定理,又称为拉格朗日定理:每个正整数都可以表示为至多 4 个正整数的平方和。如果把 0 包括进去,就正好可以表示为 4 个数的平方和。对于一个给定的正整数,可能存在多种平方和的表示法。要求对 4 个数排序,满足 (0 \leq a \leq b \leq c \leq d),并对所有的可能表示法按 (a)、(b)、(c)、(d) 为联合主键升序排列,最后输出第一个表示法。

m0_73002963的博客 1237

枚举与优化套路

hihoCoder的1494题 hihoCoder #1505题:小Hi和小Ho的礼物。#1686 : 互补二元组#1745 : 最大顺子 题目连接:#1514 : 偶像的条件 #1607 : H星人社交网络 题目链接:#1534 : Array Partition

越看越喜欢啊 2629

蓝桥杯四平方和:从O(N²)暴力到O(N)哈希与数组优化

在算法竞赛中,空间换时间与枚举降维是优化暴力解法的核心思路。以四平方和问题为例,通过将四层循环拆分为两个两层循环,并利用哈希表或数组预处理平方和,可将时间复杂度从O(N²)降至O(N)。这种折半枚举思想不仅适用于蓝桥杯等竞赛题目,也广泛应用于两数之和、前缀和等场景。哈希预处理与数组直接映射各有优劣,数组方案常数更小、内存更可控。掌握这些技巧,能有效应对大规模数据下的性能挑战,提升算法竞赛实战能力。

weixin_32642109的博客 98

每日算法打卡:四平方和 day 9

那么我们如何快速的判断一个数是否在数组中出现过,一种做法就是使用哈希,另一种就是使用二分的做法。,我们只需要判断这个t是否在之前存的数组中出现过即可,如果出现过,则找到了一组解。是从大到小的顺序,也就是查询是否存在时是从大到小查的,假设存在对应的t,那么对于。这道题的内容比较简洁,就是将一个整数分成四个整数的平方和的形式,有很多做法。一定是成立的,其次我们在枚举的时候,对于a和b是小到大枚举的,这时算出来的。存起来,之后再枚举a和b,对于每一个我们枚举的a和b,都可以计算出。

ye_yumo的博客 594
上一篇: 一道题带你领略DFS-记搜-DP
下一篇: 分巧克力-二分
CodeSlogan
博客等级 码龄6年 4227粉丝 130原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeSlogan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值