Codeforces Round 871 (Div. 4)——G题讲解

news2024/10/6 13:24:08

蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello, 大家好哇!本初中生蒟蒻讲解一下G. Hits Different!
在这里插入图片描述
上绿名喽!

===========================================================================================

G. Hits Different

题目描述

In a carnival game, there is a huge pyramid of cans with 2023 rows, numbered in a regular pattern as shown.

在这里插入图片描述

If can 9 2 9^2 92 is hit initially, then all cans colored red in the picture above would fall.

You throw a ball at the pyramid, and it hits a single can with number n 2 n^2 n2. This causes all cans that are stacked on top of this can to fall (that is, can n 2 n^2 n2 falls, then the cans directly above n 2 n^2 n2 fall, then the cans directly above those cans, and so on). For example, the picture above shows the cans that would fall if can 9 2 9^2 92 is hit.

What is the sum of the numbers on all cans that fall? Recall that n 2 = n × n n^2=n×n n2=n×n.

Input

The first line contains an integer t ( 1 ≤ t ≤ 1000 ) t (1≤t≤1000) t(1t1000) — the number of test cases.

The only line of each test case contains a single integer n ( 1 ≤ n ≤ 1 0 6 ) n (1≤n≤10^6) n(1n106) — it means that the can you hit has label n 2 n^2 n2.

Output

For each test case, output a single integer — the sum of the numbers on all cans that fall.

Please note, that the answer for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). For all valid inputs, the answer will always fit into 64-bit integer type.

Example

在这里插入图片描述
Note
The first test case is pictured in the statement. The sum of the numbers that fall is
1 2 + 2 2 + 3 2 + 5 2 + 6 2 + 9 2 = 1 + 4 + 9 + 25 + 36 + 81 = 156. 1^2+2^2+3^2+5^2+6^2+9^2=1+4+9+25+36+81=156. 12+22+32+52+62+92=1+4+9+25+36+81=156.
In the second test case, only the can labeled 1 2 1^2 12 falls, so the answer is 1 2 = 1 1^2=1 12=1.
In the third test case, the cans labeled 1 2 1^2 12 and 2 2 2^2 22 fall, so the answer is 1 2 + 2 2 = 1 + 4 = 5 1^2+2^2=1+4=5 12+22=1+4=5.
In the fourth test case, the cans labeled 1 2 1^2 12 and 3 2 3^2 32 fall, so the answer is 1 2 + 3 2 = 1 + 9 = 10 1^2+3^2=1+9=10 12+32=1+9=10.
In the fifth test case, the cans labeled 1 2 , 2 2 1^2, 2^2 12,22, and 4 2 4^2 42 fall, so the answer is 1 2 + 2 2 + 4 2 = 1 + 4 + 16 = 21. 1^2+2^2+4^2=1+4+16=21. 12+22+42=1+4+16=21.

思路

本题可以想到动态规划,我们可以这样考虑:
在这里插入图片描述
假设砸 1 4 2 14^2 142这个点,我们看一下怎么转移:
在这里插入图片描述
红色的是 1 4 2 14^2 142左边这个点 7 2 7^2 72这个点所能波及到的点
在这里插入图片描述
绿色的是 1 4 2 14^2 142右边的点 8 2 8^2 82所能波及到的点

然后我们看一下他们重复了那些点:
在这里插入图片描述
其实就是 1 4 2 14^2 142上一行的上一行的左边的点 4 2 4^2 42所能波及到的点

故此,砸 1 4 2 14^2 142的值应该是砸 7 2 7^2 72的值+砸 8 2 8^2 82的值-砸 4 2 4^2 42的值+ 1 4 2 14^2 142

可以推导出状态转移方程:( d p [ i ] [ j ] dp[i][j] dp[i][j]表示砸图中第i行第j列的点的答案)
d p [ i ] [ j ] = d p [ i − 1 ] [ j ] + d p [ i − 1 ] [ j − 1 ] − d p [ i − 2 ] [ j − 1 ] + n u m [ i ] [ j ] dp[i][j] = dp[i -1][j]+dp[i-1][j-1]-dp[i-2][j-1]+num[i][j] dp[i][j]=dp[i1][j]+dp[i1][j1]dp[i2][j1]+num[i][j]

其中, n u m [ i ] [ j ] num[i][j] num[i][j]表示图中第i行第j列的数字

本题就是求 d p [ 1 − 2023 ] [ 1 − 2023 ] dp[1-2023][1-2023] dp[12023][12023]的值
时间复杂度 O ( n 2 ) O(n^2) O(n2)

题中是输入的 n n n我们找到他是第几行,第几列即可!

代码

#include <iostream>
#define int long long

using namespace std;

const int N = 2024;

int dt;
int n;
int dp[N][N];
int num[N][N];
int sum[N];

signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);
	
	int t = 1;
	for (int i = 1; i <= 2023; i ++)
		for (int j = 1; j <= i; j ++)
			num[i][j] = t * t, t ++;
			
	sum[1] = 1;
	for (int i = 2; i <= 2023; i ++)
		sum[i] = sum[i - 1] + i - 1;
	
	dp[1][1] = 1;
	for (int i = 2; i <= 2023; i ++)
		for (int j = 1; j <= i; j ++)
			dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] - dp[i - 2][j - 1] + num[i][j];
	
	cin >> dt;
	
	while (dt --)
	{
		cin >> n;
		
		int h;
		for (int i = 1; i <= 2023; i ++)
			if (n < sum[i])
			{
				h = i - 1;
				break;
			}
		
		cout << dp[h][n - sum[h] + 1] << endl;
	}
	
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/499812.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SmartSoftHelp 自定义开源C#代码生成器

​​​​​​蓦然回首终结者SmartSoftHelp开发辅助工具MiniLite2.0迷你版 V3.5 自定义生成 dbhelper Model BLL DAL sqltxt UI 方便快捷&#xff0c;支持自编码&#xff0c;自编译&#xff0c;自己修改生成代码内容和格式&#xff0c;方便快捷... 开源代码生成说明: 1.目录…

python+django汽车4S店零配件保养服务管理系统

汽车4S服务管理系统包括三种用户。管理员、普通员工、客户。 开发语言&#xff1a;Python 框架&#xff1a;django/flask Python版本&#xff1a;python3.7.7 数据库&#xff1a;mysql 数据库工具&#xff1a;Navicat 开发软件&#xff1a;PyCharm django 应用目录结构管…

vue diff算法与虚拟dom知识整理(5) 手写一个自己的h函数

本文的意义在于教会大家如何手写一个h函数 上文中 我们简单理解了一下h函数 他的作用是构建一个虚拟的dom节点 掌握这个函数还是很有必要的 首先 你想要写出来 还是得去看原版的ts代码 这边 我们没必要把太多注意力放在TS上 所以 我们这边是看ts代码 然后 仿写js代码 我们在…

Faster RCNN在pycharm中运行

文章目录 1 代码2 文件说明3 代码解析5 数据6 自制数据集6.1 创建文件夹6.2 标注图片 7 开始训练 1 代码 参考B站up主&#xff1a;霹雳吧啦Wzgit clone https://github.com/WZMIAOMIAO/deep-learning-for-image-processing 2 文件说明 首先找到faster_rcnn ├── backbone: …

PCL学习七:Features-特征

参考引用 Point Cloud Library黑马机器人 | PCL-3D点云 1. 特征描述与提取 3D 点云特征描述与提取是点云信息处理中的最基础也是最关键的部分&#xff0c;点云识别、分割、重采样、配准和曲面重建等大部分算法&#xff0c;都十分依赖特征描述与提取的结果 从尺度上来划分&…

MYSQL01高级_Linux版安装、各级别字符集、字符集与比较规则、SQL大小写规范

文章目录 ①. MySQL - linux版安装②. 字符集的相关操作③. 各级别的字符集④. 字符集与比较规则(了解)⑤. SQL大小写规范⑥. sql_mode的合理设置 ①. MySQL - linux版安装 ①. 进入mysql官网,找到安装文件 ②. 将抽取出来的文件放在linux下的opt下 MySQL Community Serv…

本周大新闻|15000nit亮度Micro OLED面世;印度市场再推行VR盒子

本周XR大新闻&#xff0c;AR方面&#xff0c;eMagin公布1.5万nit亮度的新款dPd Micro OLED&#xff1b;脑机技术AR眼镜Cognixion ONE获FDA认证&#xff1b;歌尔发布智能交互手环参考设计Link&#xff1b;Meta引入更多AR广告。 VR方面&#xff0c;23年Quest Gaming Showcase定于…

3D旋转 相册

效果展示 代码逻辑 <!doctype html> <html lang"en"> <head><meta charset"UTF-8"><meta name"Keywords" content""><meta name"Description" content""><title>3D旋…

Vue3(Vite) 通过 prism.js 实现代码高亮并实现Mac风格

prismjs漂亮的代码语法高亮插件 极致易用&#xff1a;引用 prism.css 和 prism.js&#xff0c;使用合适的 HTML5 标签&#xff08;code.language-xxxx&#xff09;&#xff0c;搞定&#xff01;天生伶俐&#xff1a;语言的 CSS 类是可继承的&#xff0c;所以你只需定义一次就能…

自动控制原理笔记-频率响应法-控制系统的频域稳定判据

目录 一、Nyquist稳定判据 1&#xff09;开环传递函数中没有积分环节&#xff08;不含s0的极点&#xff09; 2&#xff09;开环传递函数中含有积分环节&#xff08;含s0的开环极点&#xff09; 二、Bode 稳定判据 稳定的定义&#xff1a; 任何系统在扰动的作用下都会偏离原平衡…

炫技亮点 任务编排使用CompletableFuture优化业务流程

文章目录 背景CompletableFuture简介使用场景如何编排任务步骤场景一 多个任务串行执行场景二 多个步骤并行执行场景三 一个串行步骤后两个并行步骤场景四 一个步骤依赖两个并行步骤场景五 一个步骤依赖多个并行步骤同时完成场景六 一个任务依赖多个任务的任意一个完成结果 其他…

STL--stack queue deque

stack 一、stack介绍 stack是一种容器适配器&#xff0c;专门设计用于后进先出&#xff0c;其中元素仅从容器的一端插入和提取 二、stack接口 函数名称功能说明empty判断容器是否为空size返回容器容量大小top返回栈顶数据push从栈顶插入元素pop删除栈顶元素 三、stack模拟实…

【NLP开发】Python实现聊天机器人(若干在线聊天机器人)

文章目录 1、简介2、代码测试2.1 open.drea.cc2.2 api.ruyi.ai2.3 route.showapi.com2.4 api.binstd.com2.5 api.jisuapi.com2.6 api.fanyi.baidu.com2.7 aiml2.8 api.tianapi.com2.9 nlp.xiaoi.com2.10 api.qingyunke.com2.11 api.ownthink.com 结语 1、简介 AI 聊天机器人使…

Chrome远程调试

最近接触到Chrome远程调试相关内容&#xff0c;记录一下。 场景&#xff1a;使用Chrome远程调试Chromium。当能够控制目标主机执行命令之后&#xff0c;可以在该主机上建立全局代理&#xff0c;然后在自己这一边开启浏览器监听&#xff0c;接着在目标机器上执行 chrome.exe --…

使用CSS伪元素制作动感超酷的hover动画

css 有很多神奇的效果都是使用 CSS 伪元素利用视觉差来制作的&#xff0c;以前没怎么深入的研究过 css&#xff0c;这次复习 css 的知识点才恍然大悟&#xff0c;原来 css 这么 cool。 先上效果&#xff1a; 动画实现原理 这个组动画的实现原理很简单&#xff0c;前边是一个…

ptrace

前言 gdb 的核心技术就是使用 ptrace 系统调用。 ptrace NAMEptrace - process traceSYNOPSIS#include <sys/ptrace.h>long ptrace(enum __ptrace_request request, pid_t pid,void *addr, void *data);DESCRIPTIONThe ptrace() system call provides a means by w…

开关电源学习总结

本篇文章主要通过理论来大体的讲一下开关电源的设计的思考过程&#xff0c;希望对大家可以有所帮助。本人小白&#xff0c;如有质疑&#xff0c;可以评论区指出。 一、开关电源指标 1.输入电压与输入功率 在设计开关电源时&#xff0c;需要根据需求来设计输入电压和输入功率…

12 KVM虚拟机配置-配置虚拟设备(网络设备)

文章目录 12 KVM虚拟机配置-配置虚拟设备(网络设备)12.1 概述12.2 元素介绍12.3 配置示例 12 KVM虚拟机配置-配置虚拟设备(网络设备) 12.1 概述 XML配置文件可以配置虚拟网络设备&#xff0c;包括ethernet模式、bridge模式、vhostuser模式等&#xff0c;本节介绍虚拟网卡设备…

理解生成式AI

文章目录 1、专业术语2、生成式AI3、ChatGPT1. 理解LLMRNN循环神经网络Seq2Seq模型ChatGPT与Bert区别 4、模型的生成和部署 1、专业术语 LLM&#xff1a;大型语言模型 GAI&#xff1a;通用人工智能 NLP&#xff1a;自然语言处理 CNN&#xff1a;卷积神经网络 RNN&#xff…

Spark的安装和配置

Spark的安装和配置 推荐按照我的博客下载hadoop&#xff0c;spark&#xff0c;pyspark以及scala这样版本搭配更好。 如果觉得自己不会版本搭配可私聊博主。 先安装hadoop再安装spark scala的安装和配置&#xff1a;https://blog.csdn.net/weixin_41957626/article/details/1305…