当前位置:网站首页>Yyds dry goods inventory solution sword finger offer: cut rope (advanced version)
Yyds dry goods inventory solution sword finger offer: cut rope (advanced version)
2022-06-27 13:16:00 【51CTO】
1. sketch :
describe
I'll give you a length of n The rope of , Please cut the rope to the whole length m paragraph ( m 、 n Are integers. , n > 1 also m > 1 , m <= n ), The length of each rope is recorded as k[1],...,k[m] . Excuse me, k[1]*k[2]*...*k[m] What's the maximum possible product ? for example , When the length of the rope is 8 when , We cut it into lengths of 2、3、3 Three paragraphs of , The maximum product we get here is 18 .
Because the answer is too big , Yes, please. 998244353 modulus .
Data range : Advanced : Spatial complexity
, Time complexity
Example 1
Input :
Return value :
explain :
Example 2
Input :
Return value :
explain :
Example 3
Input :
Return value :
2. Code implementation :
import java.util.*;
public class Solution {
private long mod = 998244353;
// Fast multiplication
private long fast(long x, long y){
long res = 0;
x %= mod;
y %= mod;
while(y != 0){
if((y & 1L) != 0){
// Addition instead of multiplication , To prevent cross-border
res += x;
if(res >= mod)
res -= mod;
}
y = y >> 1;
x = x << 1;
if(x >= mod)
x -= mod;
}
return res;
}
// Fast power
long Pow(long x, long y){
long res = 1;
while(y != 0){
// You can go up one more
if((y & 1L) != 0)
res = fast(res, x);
// superposition
x = fast(x, x);
// Reduce the number of times
y = y >> 1;
}
return res;
}
public long cutRope (long number) {
// No more than 3 Direct calculation
if(number <= 3)
return number - 1;
// aliquot 3
if(number % 3 == 0)
return Pow(3, number / 3);
// Last but not least 1
else if(number % 3 == 1)
//4*3^{n-1}
return fast(Pow(3, number / 3 - 1), 4);
// Last but not least 2
else
//2*3^n
return fast(Pow(3, number / 3), 2);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
边栏推荐
- Tiktok practice ~ public / private short video interchange
- scrapy
- Failed to execute NPM instruction, prompting ssh: Permission denied
- 云原生(三十) | Kubernetes篇之应用商店-Helm
- C语言 函数指针与回调函数
- 内网学习笔记(8)
- Using FRP tool to realize intranet penetration
- Socket blocking and non blocking modes
- ThreadLocal 源码全详解(ThreadLocalMap)
- Two TCP flow control problems
猜你喜欢

Pre training weekly issue 51: reconstruction pre training, zero sample automatic fine tuning, one click call opt

基于JSP实现医院病历管理系统

gcc编译动态库和静态库
![[dynamic programming] - Knapsack Problem](/img/27/c48284f15e3f80305d7ce7c02d4378.png)
[dynamic programming] - Knapsack Problem

PLM还能怎么用?

Embedded development: embedded foundation callback function

How to choose LAN instant messaging software

Make learning pointer easier (1)

阿里一个面试题:使用两个线程,交替输出字母和数字

Postman如何设置成中文?(汉化)
随机推荐
Deeply convinced plan X - system foundation summary
基于STM32设计的蓝牙健康管理设备
VS调试技巧
Today's sleep quality record 78 points
如何修改 node_modules 里的文件
How to modify a node_ Files in modules
After 2 years of outsourcing, I finally landed! Record my ByteDance 3 rounds of interviews, hope to help you!
Local visualization tool connects to redis of Alibaba cloud CentOS server
阿胖的操作记录
printf不定长参数原理
执行 npm 指令失败,提示ssh: ... Permission denied
Threejs' ambient light + point light + parallel light + spherical light and Hepler understanding + shadow ()
与生活握手言和
诗歌一首看看
OpenHGNN发布0.3版本
Prometheus 2.26.0 新特性
l六月集训(第27天) —— 图
Failed to execute NPM instruction, prompting ssh: Permission denied
Teach you how to build a permanent personal server!
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)