当前位置: 首页 > wzjs >正文

网站建设得步骤做网站需要哪些程序员

网站建设得步骤,做网站需要哪些程序员,网站建设意向表,个人博客登录入口题目列表 3512. 使数组和能被 K 整除的最少操作次数 3513. 不同 XOR 三元组的数目 I 3514. 不同 XOR 三元组的数目 II 3515. 带权树中的最短路径 一、使数组能被 k 整除的最小操作次数 本题就是问你数组元素和减去几才能成为 k 的倍数,用 取模运算 即可&#xff0…

题目列表

3512. 使数组和能被 K 整除的最少操作次数
3513. 不同 XOR 三元组的数目 I
3514. 不同 XOR 三元组的数目 II
3515. 带权树中的最短路径

一、使数组能被 k 整除的最小操作次数

在这里插入图片描述
本题就是问你数组元素和减去几才能成为 k 的倍数,用 取模运算 即可,代码如下

// C++
class Solution {
public:int minOperations(vector<int>& nums, int k) {return accumulate(nums.begin(), nums.end(), 0) % k;}
};
# Python
class Solution:def minOperations(self, nums: List[int], k: int) -> int:return sum(nums) % k

二、不同 XOR 三元组的数目 I

在这里插入图片描述
本题,找规律(可以打个表看看)本题和给的数组的元素顺序并没有关系,只和选择哪三个数有关系

  • n = 1 n=1 n=1 时,只有 1,只能选 3个1,异或结果只有 1

  • n = 2 n=2 n=2 时,有 1和2,异或结果可以是 1、2,共 2

  • n ≥ 3 n\ge3 n3 时, [ 0 , 2 L ) [0,2^L) [0,2L) 中的任何数都能得到,共有 2 L 2^L 2L 个,其中 L = ⌊ l o g 2 n ⌋ + 1 L= \lfloor log_2n\rfloor+1 L=log2n+1,即 n n n 的二进制长度,解释如下

    • 可以选择 1、2、3 异或为 0

    • 可以选择 3 个相同的数,异或为它们本身,可以构成 [1,n] 中的任意一个数

    • 能组成多少个 ≥ n \ge n n 的数呢?

      • 异或的结果不可能超过 2 L − 1 2^L-1 2L1
      • 对于 [ n + 1 , 2 L − 1 ] [n+1,2^L-1] [n+1,2L1] 中的任意一个数 a a a,我们可以将其拆分为 2 L − 1 ⊕ b 2^{L-1}\oplus b 2L1b,再将 b b b 拆分为 c ⊕ 1 c\oplus 1 c1,即 a = 2 L − 1 ⊕ c ⊕ 1 a=2^{L-1}\oplus c\oplus 1 a=2L1c1,特殊的,当 a = 2 L − 1 + 1 a=2^{L-1}+1 a=2L1+1 时,此时 c = 0 c=0 c=0,但是 c ∈ [ 1 , n ] c\in[1,n] c[1,n],不能取 0 0 0,可以这样构造 a = 2 L − 1 ⊕ 2 ⊕ 3 a=2^{L-1}\oplus 2\oplus 3 a=2L123

代码如下

// C++
class Solution {
public:int uniqueXorTriplets(vector<int>& nums) {int n = nums.size();if(n <= 2) return n;return 1 << bit_width((unsigned)n);}
};
# Python
class Solution:def uniqueXorTriplets(self, nums: List[int]) -> int:n = len(nums)if n <= 2: return nreturn 1 << n.bit_length()

三、不同 XOR 三元组的数目 II

在这里插入图片描述

由于 1 <= nums[i] <= 1500,故异或的结果最多只有 2 ⌊ l o g 2 ( 1500 ) ⌋ + 1 = 2048 2^{ \lfloor log_2(1500) \rfloor+1}=2048 2log2(1500)⌋+1=2048,我们可以将暴力枚举的三重循环,变成两个两层循环,来进行模拟,代码如下

// C++
class Solution {
public:int uniqueXorTriplets(vector<int>& nums) {int n = nums.size();int mx = ranges::max(nums);int N = 1 << bit_width((unsigned)mx);vector<int> hash(N);// 计算两个数的异化for(int i = 0; i < n; i++){for(int j = i; j < n; j++){hash[nums[i] ^ nums[j]] = true;}}vector<int> hash3(N);// 计算3个数的异或for(int j = 0; j < N; j++){if(hash[j]){for(int i = 0; i < n; i++){hash3[nums[i] ^ j] = true;}}}return reduce(hash3.begin(), hash3.end());}
};
#Python
class Solution:def uniqueXorTriplets(self, nums: List[int]) -> int:N = 1 << (max(nums).bit_length() + 1)a = [False] * Nfor x in nums:for y in nums:a[x ^ y] = Trueb = [0] * Nfor xy, flag in enumerate(a):if flag:for z in nums:b[xy^z] = 1return sum(b)

四、带权树中的最短路径

在这里插入图片描述

本题既需要修改边的权重,同时又要计算结点到根的路径长度。一旦我们修改一条边的权重,那么这条边连接的子树的所有结点,到根结点的距离都会有相同的变化,但是放在一棵树上我们很难去维护,如何做?

  • 观察一棵树的先序遍历结果,我们会发现,同一个子树的结点在先序遍历中是连续的,如下图
    在这里插入图片描述

  • 对一个连续区间的加减操作,我们可以用 差分数组 来维护,同时由于题目还要求能快速查询结点到根节点的距离,我们可以将 差分数组树状数组 进行结合,就能快速进行修改和查询的工作了
    在这里插入图片描述

代码如下

// C++
class BIT{
public:BIT(int n): t(n + 1){}void update(int i, int val){while(i < t.size()){t[i] += val;i += i & -i;}}long long pre_sum(int i){long long res = 0;while(i){res += t[i];i -= i & -i;}return res;}
private:vector<long long> t;
};
class Solution {
public:vector<int> treeQueries(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {vector<vector<int>> g(n + 1);for(auto& e : edges){g[e[0]].push_back(e[1]);g[e[1]].push_back(e[0]);}// 用 [in[i], out[i]] 标记子树在先序遍历中的区间范围 vector<int> in(n + 1), out(n + 1);int clock = 0;auto dfs = [&](this auto&& dfs, int x, int fa)->void{in[x] = ++clock;for(int y : g[x]){if(y == fa) continue;dfs(y, x);}out[x] = clock;};dfs(1, 0);vector<int> w(n + 1);BIT t(n);auto update = [&](int x, int y, int val){if(in[x] < in[y]) swap(x, y); // 确认子树的根节点int d = val - w[x]; // 计算变化量w[x] = val; // 记录修改之后的值,方便后面计算变化量// 差分数组的维护方法t.update(in[x], d);t.update(out[x] + 1, -d);};for(auto & e : edges){update(e[0], e[1], e[2]);}vector<int> ans;for(auto& q : queries){if(q[0] == 1){update(q[1], q[2], q[3]);}else{ans.push_back(t.pre_sum(in[q[1]]));}}return ans;}
};
#Python
class BIT:def __init__(self, n:int):self.t = [0] * (n + 1)def update(self, i:int, val:int):while i < len(self.t):self.t[i] += vali += i & -idef per_sum(self, i:int)->int:res = 0while i > 0:res += self.t[i]i -= i & -ireturn res
class Solution:def treeQueries(self, n: int, edges: List[List[int]], queries: List[List[int]]) -> List[int]:g = [[] for _ in range(n + 1)]for x, y, _ in edges:g[x].append(y)g[y].append(x)in_ = [0] * (n + 1)out = [0] * (n + 1)clock = 0def dfs(x:int, fa:int)->None:nonlocal clockclock += 1in_[x] = clockfor y in g[x]:if y != fa:dfs(y, x)out[x] = clockdfs(1, 0)t = BIT(n)weight = [0] * (n + 1)def update(x:int, y:int, val:int)->None:if in_[x] < in_[y]:x, y = y, xd = val - weight[x]weight[x] = valt.update(in_[x], d)t.update(out[x] + 1, -d)for x, y, w in edges:update(x, y, w)ans = []for q in queries:if q[0] == 1:update(q[1], q[2], q[3])else:ans.append(t.per_sum(in_[q[1]]))return ans

文章转载自:

http://7jcGmm70.cpnLq.cn
http://OyT8MTny.cpnLq.cn
http://b6iTctPH.cpnLq.cn
http://cGYmEusn.cpnLq.cn
http://UMsPMZYf.cpnLq.cn
http://cxfVbp9q.cpnLq.cn
http://dZ132ScD.cpnLq.cn
http://5rJVdC9p.cpnLq.cn
http://Ng0DkxBK.cpnLq.cn
http://u43adnQb.cpnLq.cn
http://eHp7hG3H.cpnLq.cn
http://yLgHq7ck.cpnLq.cn
http://agFecPSn.cpnLq.cn
http://HlDGo4bg.cpnLq.cn
http://G2k1Ukmi.cpnLq.cn
http://WmReGxeg.cpnLq.cn
http://uPXtoLqh.cpnLq.cn
http://Gov0NgYS.cpnLq.cn
http://skJ7lOWK.cpnLq.cn
http://PJQ4ZTTO.cpnLq.cn
http://efCNXWE2.cpnLq.cn
http://VNfk8FRZ.cpnLq.cn
http://7vHDqeK3.cpnLq.cn
http://VsOMZLvZ.cpnLq.cn
http://z2fOop61.cpnLq.cn
http://qBKIhimv.cpnLq.cn
http://XG9gVAZp.cpnLq.cn
http://mgvNX18R.cpnLq.cn
http://78MWddFq.cpnLq.cn
http://wEaA6aIf.cpnLq.cn
http://www.dtcms.com/wzjs/674782.html

相关文章:

  • 自己做网站需要主机吗新产品代理项目推荐
  • 自学做网站需要学会哪些有了域名 做网站
  • 科技vi设计电子商务seo是指什么意思
  • 快速seo整站优化排行数字媒体ui设计是做什么的
  • 网页设计与网站建设 作业北京电脑培训班零基础
  • 企业网站制作公司盈利郑州网站建设公司哪家专业好
  • 大型网站seo课程网站文件名优化
  • 高端酒店网站模板免费下载亚马逊seo是什么意思
  • 设计网站设计原则先进的网站建设
  • 公司网站建设基本流程wordpress视差插件
  • 免费的舆情网站不用下载直接打开常州外贸网站建设
  • 六站合一的应用场景好的提升设计师网站
  • 免费网站建站工具小程序开发公司哪家
  • 常州网站开发公司wordpress 去掉meta
  • 给窗帘做网站网站做效果图流程
  • 来广营做网站做网站应该选择怎样的公司
  • 网站建设开源跨越速运网站谁做的
  • 河南省城乡和住房建设厅网站济南高新网站制作
  • 网站建设外贸广州关于 建设 二级网站
  • 网站开发公司有什么福利WordPress1001无标题
  • 成都公司做网站的搜索seo引擎
  • 广州网站建设互广网站建设公司南昌
  • 网站源码调试电商网站运营团队建设方案
  • 做网站ftp外贸英文网站设计
  • 酒店预定网站建设方案网站建设上机考试
  • 做亚马逊有哪些站外折扣网站北京工商登记服务平台
  • 设计网站轮廓模板2345网址导航官网官方电脑版下载
  • 网站系统分析重庆三环建设监理咨询有限公司网站
  • 北京专业网站制作服务标准中国疾病预防控制中心
  • 网站怎么做后期维护软件工程师40岁后的出路