LeetCode //C - 970. Powerful Integers

LeetCode //C - 970. Powerful Integers

970. Powerful Integers

Given three integers x, y, and bound, return a list of all the powerful integers that have a value less than or equal to bound.

An integer is powerful if it can be represented as x i + y j x^i + y^j xi+yjfor some integers i >= 0 and j >= 0.

You may return the answer in any order. In your answer, each value should occur at most once.
 

Example 1:
Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation:
2 = 2 0 + 3 0 2 = 2^0 + 3^0 2=20+30
3 = 2 1 + 3 0 3 = 2^1 + 3^0 3=21+30
4 = 2 0 + 3 1 4 = 2^0 + 3^1 4=20+31
5 = 2 1 + 3 1 5 = 2^1 + 3^1 5=21+31
7 = 2 2 + 3 1 7 = 2^2 + 3^1 7=22+31
9 = 2 3 + 3 0 9 = 2^3 + 3^0 9=23+30
10 = 2 0 + 3 2 10 = 2^0 + 3^2 10=20+32
Example 2:
Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]
Constraints:
  • 1 <= x, y <= 100
  • 0 < = b o u n d < = 10 6 0 <= bound <= 10^6 0<=bound<=106

From: LeetCode
Link: 970. Powerful Integers


Solution:

Ideas:
  • A powerful integer is any number that can be written as x^i + y^j where i, j ≥ 0.
  • Precompute all powers of x and y that do not exceed bound.
  • Special case: if x == 1 or y == 1, only one power (1) exists for that base.
  • Combine every x^i with every y^j and compute the sum.
  • Use a seen array (or hash set) to avoid duplicates.
  • Only keep sums ≤ bound.
  • Collect all marked sums and return them as the final list.
Code:
/** * Note: The returned array must be malloced, assume caller calls free(). */int*powerfulIntegers(int x,int y,int bound,int* returnSize){if(bound <2){// smallest possible sum is 1^0 + 1^0 = 2*returnSize =0;returnNULL;}// Precompute powers of x and y up to `bound`int xp[32], yp[32];int xn =0, yn =0;int val =1;while(val <= bound){ xp[xn++]= val;if(x ==1)break;// further powers would be the sameif(val > bound / x)break; val *= x;} val =1;while(val <= bound){ yp[yn++]= val;if(y ==1)break;if(val > bound / y)break; val *= y;}// Use a boolean array to mark which sums have appeared (to avoid duplicates)char*seen =(char*)calloc(bound +1,sizeof(char));if(!seen){*returnSize =0;returnNULL;// allocation failure (unlikely on LeetCode)}int count =0;for(int i =0; i < xn;++i){for(int j =0; j < yn;++j){int sum = xp[i]+ yp[j];if(sum > bound)continue;if(!seen[sum]){ seen[sum]=1; count++;}}}int*res =(int*)malloc(sizeof(int)* count);if(!res){free(seen);*returnSize =0;returnNULL;}// Collect sums in any order; here we choose increasing orderint idx =0;for(int s =2; s <= bound;++s){if(seen[s]){ res[idx++]= s;}}free(seen);*returnSize = count;return res;}

Read more

Flutter 三方库 async_extension 的鸿蒙化适配指南 - 实现具备高级异步编排算法与流操作扩展的并发工具集、支持端侧复杂业务流的函数式处理实战

Flutter 三方库 async_extension 的鸿蒙化适配指南 - 实现具备高级异步编排算法与流操作扩展的并发工具集、支持端侧复杂业务流的函数式处理实战

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.ZEEKLOG.net Flutter 三方库 async_extension 的鸿蒙化适配指南 - 实现具备高级异步编排算法与流操作扩展的并发工具集、支持端侧复杂业务流的函数式处理实战 前言 在进行 Flutter for OpenHarmony 的大规模异步业务系统(如实时行情刷新、多源数据聚合)开发时,如何更优雅地处理 Future 的超时竞争、Stream 的防抖(Debounce)或复杂的并发队列控制?虽然 Dart async 包提供了基础功能,但 async_extension 进一步扩展了异步编程的边界,提供了更符合函数式范式的工具。本文将探讨如何在鸿蒙端构建极致、高效的异步处理链路。 一、原直观解析 / 概念介绍 1.1 基础原理 该库通过对 Dart 核心异步类的非侵入式扩展(Extensions)

By Ne0inhk
计算机毕业设计必看必学~基于springboot大学生实习管理系统的设计与实现,原创定制程序、单片机、java、PHP、Python、小程序、文案全套、毕设成品等!

计算机毕业设计必看必学~基于springboot大学生实习管理系统的设计与实现,原创定制程序、单片机、java、PHP、Python、小程序、文案全套、毕设成品等!

springboot大学生实习管理系统 摘要 随着大学生实习的日益重要和广泛普及,建立一套高效、便捷的大学生实习管理系统对于高校和学生都具有重要意义。本文基于Spring Boot框架,设计并开发了一套大学生实习管理系统,旨在提供一个全面、可靠的平台,方便学生、教师和企业进行实习管理。 该系统采用了前后端分离的架构,前端使用Vue.js技术栈开发,后端使用Spring Boot框架搭建。系统实现了学生信息管理、实习岗位发布与申请、实习评分等功能。学生可以通过系统查看自己的实习信息、提交实习报告和评价,并与企业和指导老师进行沟通交流。教师可以管理学生的实习任务、审核实习报告以及评定实习成绩。企业可以发布实习岗位、筛选学生申请,并进行实习任务的管理和评价。 在系统实现过程中,充分考虑了用户体验和安全性。通过合理的权限控制机制,确保不同角色用户只能访问其具备权限的功能。同时,系统也提供了数据备份和恢复功能,保障数据的安全性和可靠性。 实际测试结果表明,该系统具有良好的稳定性和可用性,满足了大学生实习管理的需求。用户对系统的易用性和功能完善性给予了积极评价。 综上所述,本文设计和开

By Ne0inhk
vue3+python基于python的球类体育赛事发布和在线购票选座系统60576715

vue3+python基于python的球类体育赛事发布和在线购票选座系统60576715

目录 * 技术栈与项目概述 * 系统架构设计 * 数据库模型设计 * 前端关键技术实现 * 后端核心逻辑 * 安全与性能优化 * 测试与部署 * 扩展方向 * 开发技术路线 * 源码lw获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! 技术栈与项目概述 * 前端框架: Vue 3 (Composition API + TypeScript) * 后端语言: Python (FastAPI/Django 选型分析) * 数据库: PostgreSQL/MySQL 与 Redis 缓存 * 核心功能: 赛事发布、在线选座购票、支付集成、实时数据更新 系统架构设计 * 前后端分离: RESTful API 接口设计规范 * 微服务模块划分: 用户服务、赛事管理、订单支付、座位库存 * WebSocket 应用: 实时推送座位锁定状态与赛事更新

By Ne0inhk

【Python】python-can使用记录

前言 之前用python做了一些汽车领域CAN模块相关自动化,主要集中在软件的代码开发阶段。随着功能的成熟,逐渐想把测试的工作也自动化起来,后来就写了个简单的上位机,支持Trace、Graph、Log、UDS、刷写等功能,可加载自定义py测试脚本。 近期突然想到能不能用canoe软件做上位机,再结合其他总线设备的桥接方式,从网上真找到了方案,尝试过后发现很棒。 不论是一开始的上位机,还是后面的桥接工具,都是基于这个python-can去做的开发,所以在此记录和分享下这个模块。 1 安装 1. 设备的CAN驱动需要自行手动安装。 直接使用pip安装python-can pip install python-can 可在: pypi.org搜索“python-can”,查阅手册(链接),里面有详细的功能讲解以及支持的硬件设备。 2 功能说明 2.1 bus的实例化(创建) 2.1.1 Bus类 can.interface.Bus是比较关键的一个类,用于创建bus实体控制总线的行为。通俗地讲就是打开硬件设备,

By Ne0inhk