当前位置:网站首页>AcWing 2456. 记事本
AcWing 2456. 记事本
2022-07-06 08:59:00 【Easenyang】
题目描述
最初在一个记事本上只有一个字符 A。
你每次可以对这个记事本进行两种操作:
Copy All (复制全部) :
你可以复制这个记事本中的所有字符(部分的复制是不允许的)。
Paste (粘贴) :
你可以粘贴你上一次复制的字符。 给定一个数字 n。
你需要使用最少的操作次数,在记事本中打印出恰好 n 个 A。
输出能够打印出 n 个 A 的最少操作次数。
输入格式
一个整数 n。
输出格式
一个整数,表示最少操作次数。
数据范围
1 ≤ n ≤ 1 0 6 1≤n≤10^6 1≤n≤106
题目链接:记事本
思路:
我们先考虑输入的数为质数 n 的情况:质数肯定是必须要先复制 1 次,然后粘贴 n - 1次。(例如 7 只能是复制 1 次,粘贴 6 次得到,大家可以在草稿本上推一次)
那么对于合数的情况,我们就可以去分解质因数,然后不断粘贴得到。
举个例子:
24 = 22 * 32。我们先去复制粘贴得到2,复制 1 次,粘贴 2 - 1次,得到 22 就又要复制 1 次,粘贴 2-1次,然后把22当成整体,复制 1 次,粘贴 3-1次,再复制 1 次,粘贴 2-1 次。共2x2+3x2 = 10次
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
int res = 0;;
for (int i = 2; i <= n; i++) {
int s = 0; //看当前质因数用到了几次
while (n % i == 0) {
s++;
n /= i;
}
if (s > 0) {
res += i * s;
}
}
System.out.println(res);
}
}
边栏推荐
- LeetCode:387. 字符串中的第一个唯一字符
- LeetCode:39. Combined sum
- [MySQL] limit implements paging
- IJCAI2022论文合集(持续更新中)
- 什么是MySQL?MySql的学习之路是怎样的
- I-BERT
- LeetCode:394. String decoding
- [oc foundation framework] - < copy object copy >
- Current situation and trend of character animation
- How to effectively conduct automated testing?
猜你喜欢

数字人主播618手语带货,便捷2780万名听障人士

【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
![[oc]- < getting started with UI> -- common controls uibutton](/img/4d/f5a62671068b26ef43f1101981c7bb.png)
[oc]- < getting started with UI> -- common controls uibutton

postman之参数化详解

不同的数据驱动代码执行相同的测试场景
![[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born](/img/70/d275009134fcbf9ae984c0f278659e.jpg)
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born

SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

Selenium+pytest automated test framework practice

甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
随机推荐
Selenium+Pytest自动化测试框架实战
Cesium draw points, lines, and faces
Navicat premium create MySQL create stored procedure
KDD 2022论文合集(持续更新中)
使用标签模板解决用户恶意输入的问题
LeetCode:124. 二叉树中的最大路径和
Alibaba cloud server mining virus solution (practiced)
Intel Distiller工具包-量化实现2
CUDA实现focal_loss
A convolution substitution of attention mechanism
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Intel Distiller工具包-量化实现1
KDD 2022 paper collection (under continuous update)
CUDA realizes focal_ loss
[oc]- < getting started with UI> -- learning common controls
TP-LINK 企业路由器 PPTP 配置
LeetCode:214. 最短回文串
使用latex导出IEEE文献格式
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
LeetCode:剑指 Offer 04. 二维数组中的查找