前缀和多种基础

server/2025/2/5 0:47:59/

前缀和加法

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
const int N = 1e3+10;
int arr[N];
int pre[N];
int org[N];
int main(void)
{cin >> n;for(int i = 1 ; i <= n ; i++){cin >> arr[i];pre[i] = pre[i-1] + arr[i];}cout << "输出前缀和数组:"<<endl;for(int i = 0 ; i <= n ; i++)cout << pre[i] << " ";cout << endl; //输出下标1~2的区间和int l = 1,r = 2;cout << "输出下标1~2的区间和:" << pre[r]-pre[l-1]; cout << endl;//根据前缀和复原初始数组for(int i = 1 ; i <= n ; i++)org[i] = pre[i] - pre[i-1];cout << "输出原始数组:";for(int i = 1 ; i <= n ; i++)cout << org[i] << " ";	return 0;
}
/*
11
8 5 3 2 1 5 6 9 4 3 2
*/

前缀积

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
const int N = 1e3+10;
int arr[N];
int pre[N];
int ori[N]; 
const int mod = 2;
int main(void)
{cin >> n;pre[0] = 1;//初始化为1 for(int i = 1 ; i <= n ; i++){cin >> arr[i];pre[i] = (pre[i-1] * arr[i]);//pre[i] = (pre[i-1] * arr[i])%mod}cout << "输出前缀积数组:"<<endl;for(int i = 0 ; i <= n ; i++)cout << pre[i] << " ";cout << endl; //输出下标1~2的区间和int l = 1,r = 2;cout << "输出下标1~2的区间积:" << pre[r]/pre[l-1]; //没有取模的情况可以直接使用 cout << endl; for(int i = 1 ; i <= n ; i++)ori[i] = pre[i]/pre[i-1];cout << "输出原始数组:";for(int i = 1 ; i <= n ; i++)cout << ori[i] << " ";return 0;
}
/*
11
8 5 3 2 1 5 6 9 4 3 2
*/

前缀异或

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
const int N = 1e3+10;
int arr[N];
int pre[N];
int ori[N];
int main(void)
{cin >> n;pre[0] = 0;//0为异或的恒等元 for(int i = 1 ; i <= n ; i++){cin >> arr[i];pre[i] = (pre[i-1] ^ arr[i]);}cout << "输出前缀异或数组:"<<endl;for(int i = 0 ; i <= n ; i++)cout << pre[i] << " ";cout << endl; //输出下标1~2的区间和int l = 1,r = 2;cout << "输出下标1~2的区间异或:" << (pre[r]^pre[l-1]);cout << endl; for(int i = 1 ; i <= n ; i++)ori[i] = pre[i]^pre[i-1];cout << "输出前缀异或数组:";for(int i = 1 ; i <= n ; i++)cout << ori[i] << " "; return 0;
}
/*
11
8 5 3 2 1 5 6 9 4 3 2
*/

后缀和:

一个数组包含了非负整数,要求从某个点开始,直到末尾和直到0的和。

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
const int N = 1e3+10;
int arr[N];
int suffix[N];
int org[N];
int main(void)
{cin >> n;for(int i = 1 ; i <= n ; i++)cin >> arr[i];for(int i = n ; i >= 1 ; i--){if(arr[i]){suffix[i] = suffix[i+1];//先加上后一个后缀和 suffix[i] += arr[i]; //加入arr[i]的元素 }}for(int i = 1 ; i <= n ; i++)cout << suffix[i] << " ";return 0;
}
/*
8
1 2 0 3 4 1 0 5
正确答案
3 2 0 8 5 1 0 5 
*/


http://www.ppmy.cn/server/165024.html

相关文章

机器学习day5

自定义数据集 使用tensorflow框架实现逻辑回归并保存模型&#xff0c;然后保存模型后再加载模型进行预测 代码 import tensorflow as tf import numpy as np# 1. 自定义数据集 data [[-0.5, 7.7], [1.8, 98.5], [0.9, 57.8], [0.4, 39.2], [-1.4, -15.7], [-1.4, -37.3], [-1…

SpringBoot整合Mybatis|入门级增删改查|2025

SpringBoot整合Mybatis 文章目录 SpringBoot整合Mybatis1. 新建User表2. 初始化项目2.1 新建项目2.2 配置数据库连接2.3 完善项目的架子 3. 正式开始3.1 新增用户3.2 根据邮箱查询3.4 改密码 和 删除用户3.5 用xml再写一遍 4. 进阶 1. 新建User表 CREATE DATABASE mybatis_dem…

音视频入门基础:RTP专题(7)——RTP协议简介

一、引言 本文对RTP协议进行简介。在简介之前&#xff0c;请各位先下载RTP的官方文档《RFC 3550》和《RFC 3551》。《RFC 3550》总共有89页&#xff0c;《RFC 3551》总共有44页。本文下面所说的“页数”是指在pdf阅读器中显示的页数&#xff1a; 二、RTP协议简介 根据《RFC 35…

nvm的安装和使用

打开地址下载 https://github.com/coreybutler/nvm-windows/releases 推荐下载&#xff0c;nvm-setup.zip 这个。可能有的教程会让下载nvm-noinstall.zip 。noinstall确实下载之后不用安装&#xff0c;但是要自己配置setting.txt文件&#xff0c;以及环境变量 。 安装nvm 在电…

深度学习-98-大语言模型LLM之基于langchain的代理create_react_agent工具

文章目录 1 Agent代理1.1 代理的分类1.2 ReAct和Structured chat2 代理应用ReAct2.1 创建工具2.1.1 嵌入模型2.1.2 创建检索器2.1.3 测试检索结果2.1.4 创建工具列表2.2 初始化大模型2.3 创建Agent2.4 运行Agent3 参考附录1 Agent代理 Agent代理的核心思想是使用语言模型来选择…

数据结构 树2

文章目录 前言 一&#xff0c;二叉搜索树的高度 二&#xff0c;广度优先VS深度优先 三&#xff0c;广度优先的代码实现 四&#xff0c;深度优先代码实现 五&#xff0c;判断是否为二叉搜索树 六&#xff0c;删除一个节点 七&#xff0c;二叉收索树的中序后续节点 总结 …

Go学习:格式化输入输出

目录 1. 输出 2. 输入 1. 输出 常用格式&#xff1a; 格式说明%d整型格式%s字符串格式%c字符格式%f浮点数格式%T操作变量所属类型%v自动匹配格式输出 简单示例代码&#xff1a; package mainimport "fmt"func main() {a : 10b : "abc"c : ad : 3.14/…

【深度分析】DeepSeek大模型技术解析:从架构到应用的全面探索

深度与创新&#xff1a;AI领域的革新者 DeepSeek&#xff0c;这个由幻方量化创立的人工智能公司推出的一系列AI模型&#xff0c;不仅在技术架构上展现出了前所未有的突破&#xff0c;更在应用领域中开启了无限可能的大门。从其混合专家架构&#xff08;MoE&#xff09;到多头潜…