当前位置:网站首页>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:214. 最短回文串
- Esp8266-rtos IOT development
- CUDA realizes focal_ loss
- Advanced Computer Network Review(5)——COPE
- LeetCode:498. Diagonal traversal
- Pytest之收集用例规则与运行指定用例
- Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
- Advance Computer Network Review(1)——FatTree
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- 多元聚类分析
猜你喜欢
MySQL uninstallation and installation methods
Esp8266-rtos IOT development
Computer graduation design PHP Zhiduo online learning platform
Variable length parameter
数学建模2004B题(输电问题)
Deep anatomy of C language -- C language keywords
[OC-Foundation框架]--<Copy对象复制>
CUDA implementation of self defined convolution attention operator
TP-LINK 企业路由器 PPTP 配置
TP-LINK enterprise router PPTP configuration
随机推荐
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
requests的深入刨析及封装调用
Unsupported operation exception
[MySQL] multi table query
Current situation and trend of character animation
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
SimCLR:NLP中的对比学习
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
IJCAI2022论文合集(持续更新中)
[OC]-<UI入门>--常用控件的学习
随手记01
注意力机制的一种卷积替代方式
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
LeetCode41——First Missing Positive——hashing in place & swap
Leetcode: Jianzhi offer 03 Duplicate numbers in array
LeetCode:劍指 Offer 42. 連續子數組的最大和
Pytest's collection use case rules and running specified use cases
Navicat Premium 创建MySql 创建存储过程
Super efficient! The secret of swagger Yapi
Philosophical enlightenment from single point to distributed