CF940B Our Tanya is Crying Out Loud

本文介绍了一个数学问题:通过两种操作(减1或除以k)将整数n变为1的最小成本。文章提供了详细的解析思路及C++实现代码。
Our Tanya is Crying Out Loud
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Right now she actually isn't. But she will be, if you don't solve this problem.

You are given integers nkA and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:

  1. Subtract 1 from x. This operation costs you A coins.
  2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
What is the minimum amount of coins you have to pay to make x equal to 1?
Input

The first line contains a single integer n (1 ≤ n ≤ 2·109).

The second line contains a single integer k (1 ≤ k ≤ 2·109).

The third line contains a single integer A (1 ≤ A ≤ 2·109).

The fourth line contains a single integer B (1 ≤ B ≤ 2·109).

Output

Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.

Examples
input
Copy
9
2
3
1
output
6
input
Copy
5
5
2
20
output
8
input
Copy
19
3
4
2
output
12
Note

In the first testcase, the optimal strategy is as follows:

  • Subtract 1 from x (9 → 8) paying 3 coins.
  • Divide x by 2 (8 → 4) paying 1 coin.
  • Divide x by 2 (4 → 2) paying 1 coin.
  • Divide x by 2 (2 → 1) paying 1 coin.

The total cost is 6 coins.

In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.

 

题目的意思是给你两种操作,一种是每次减1消耗a元,一种是每次除以k每次消耗b元。使n变成1的最小消耗。

在每次没有达到k的倍数前,只能减一,达到后判断下消耗是减去还是除以小,选小的。

 

#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 100010
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
int main() {
    ll n,k,a,b;
    while( cin >> n >> k >> a >> b ) {
        ll ans = 0;
        if( k == 1 ) {
            cout << ( n - 1 ) * a << endl;
            continue;
        }
        while( n != 1 ) {
             if( n % k == 0 ) {
                ans += min( ( n - n / k ) * a, b );
                n /= k;
             } else if( n > k ) {
                ans += ( n % k ) * a;
                n -= n % k;
             } else {
                ans += ( n - 1 ) * a;
                n = 1;
             }
        }
        cout << ans << endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/l609929321/p/8469391.html

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
<think>我们正在处理一个SpringWebFlux中的异常问题,具体是`ClassCastException:org.apache.catalina.connector.ResponseFacadecannotbecasttoreactor.netty.http.server.HttpServerResponse`。这个异常通常发生在尝试将Tomcat的`ResponseFacade`对象转换为ReactorNetty的`HttpServerResponse`对象时。这表明我们可能在错误的环境中使用了不兼容的组件。根据提供的引用,虽然引用中描述的是类似但不同的异常(例如,`RequestFacade`无法转换为`MultipartHttpServletRequest`),但解决思路可能有相似之处:配置问题或组件不匹配。在SpringWebFlux中,默认使用的是Netty作为服务器,而不是Tomcat。因此,出现这个异常很可能是因为应用程序被部署到了Tomcat容器中,但SpringWebFlux期望的是ReactorNetty的环境。解决方案:1.确保使用正确的服务器:SpringWebFlux应用应该运行在Netty上,而不是Tomcat。因此,检查并移除对Tomcat的依赖,添加Netty的依赖。2.如果确实需要运行在Tomcat上(例如,使用SpringWebFlux与Servlet3.1+容器),则需要注意,SpringWebFlux支持运行在Servlet容器上,但需要容器支持非阻塞I/O(如Tomcat8.5+,Jetty9.3+等)。在这种情况下,需要确保正确配置了适配器。然而,异常信息表明我们尝试将Tomcat的`ResponseFacade`转换为ReactorNetty的`HttpServerResponse`,这显然是不合理的,因为这两个对象分别属于不同的服务器实现。因此,问题可能出在引入了不兼容的依赖或配置。具体步骤:-检查`pom.xml`(如果使用Maven)或`build.gradle`(如果使用Gradle)中的依赖。确保没有包含Tomcat的starter依赖(如`spring-boot-starter-tomcat`),而应该使用`spring-boot-starter-webflux`,它会默认引入Netty。-如果需要在Servlet容器中运行,应该排除Netty依赖并包含Servlet容器依赖,同时使用`WebReactive`的Servlet容器支持。但是,此时不应该出现ReactorNetty的类,因为ReactorNetty不会被使用。根据异常信息,我们试图将Tomcat的响应对象转换为ReactorNetty的响应对象,这很可能是由于代码中错误地引用了ReactorNetty的类,而实际上运行在Tomcat上。因此,我们需要确保代码中不直接使用ReactorNetty的类,除非我们明确运行在Netty上。另外,查看异常堆栈,找到触发异常的代码位置。通常,这个异常发生在`ReactorNettyRequestUpgradeStrategy`中,这是用于WebSocket升级的策略,在WebFlux中,当运行在Netty上时,会使用这个策略。如果运行在Tomcat上,应该使用Tomcat自己的升级策略。因此,解决方案可能是:1.完全切换到Netty服务器(推荐,因为WebFlux与Netty是原生集成的)。2.或者,如果要在Tomcat上运行,确保不使用`ReactorNettyRequestUpgradeStrategy`,而是使用适合Tomcat的策略。在SpringBoot中,可以通过设置属性`spring.main.web-application-type=reactive`来确保使用WebFlux,并且它会自动配置Netty。同时,确保没有`spring-boot-starter-web`依赖,因为它会引入Tomcat。检查依赖:-对于Maven,确保依赖如下:```xml<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency><!--排除Tomcat(如果被引入)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency></dependencies>```或者更常见的是,只包含`spring-boot-starter-webflux`,它会自动排除`spring-boot-starter-web`(即Tomcat)。如果确实需要在Tomcat中运行,那么应该使用`spring-boot-starter-webflux`和`spring-boot-starter-tomcat`,但注意,在Tomcat中运行WebFlux是通过ServletAPI适配的,因此不会使用ReactorNetty的类。所以,在这种情况下,不应该出现`ReactorNettyRequestUpgradeStrategy`。那么,为什么会出现`ReactorNettyRequestUpgradeStrategy`呢?可能是因为在配置中显式地使用了它。检查代码,看是否有地方配置了WebSocket升级策略,并指定了`ReactorNettyRequestUpgradeStrategy`。如果有,那么在Tomcat环境中,应该使用`StandardWebSocketUpgradeStrategy`(Tomcat)或者让Spring自动配置。因此,解决方案总结:1.调整依赖,使用Netty运行(推荐),移除Tomcat依赖。2.或者,如果要在Tomcat中运行,避免使用`ReactorNettyRequestUpgradeStrategy`,使用自动配置或者适合Tomcat的策略。如果问题仍然存在,请检查是否在代码中显式创建了`ReactorNettyRequestUpgradeStrategy`实例。例如,在WebSocket配置中,我们可能这样写:```java@Configuration@EnableWebFluxpublicclassWebSocketConfigimplementsWebSocketMessageBrokerConfigurer{@OverridepublicvoidregisterStompEndpoints(StompEndpointRegistryregistry){registry.addEndpoint("/ws").setHandshakeHandler(newReactorNettyRequestUpgradeStrategy());//这里在Tomcat中运行会导致问题}}```应该改为:```java@Configuration@EnableWebFluxpublicclassWebSocketConfigimplementsWebSocketMessageBrokerConfigurer{@OverridepublicvoidregisterStompEndpoints(StompEndpointRegistryregistry){registry.addEndpoint("/ws");//不要设置RequestUpgradeStrategy,让Spring自动根据服务器环境选择}}```让Spring自动选择适合的升级策略。综上所述,解决这个异常的关键是确保服务器环境与使用的组件一致,并避免在代码中硬编码特定服务器的实现类。相关问题:1.如何在SpringBoot中从Tomcat切换到Netty?2.如何配置SpringWebFlux以在Tomcat上运行?3.在SpringWebFlux中如何正确配置WebSocket?</think>### Spring WebFlux ReactorNettyRequestUpgradeStrategy ClassCastException 解决方案 这个异常`ClassCastException: org.apache.catalina.connector.ResponseFacade cannot be cast to reactor.netty.http.server.HttpServerResponse`通常发生在以下场景: 1. **服务器环境不匹配**:应用部署在Tomcat(Servlet容器)但使用了Reactor Netty专属组件 2. **依赖冲突**:同时引入了Servlet容器和Netty的依赖 3. **错误的手动配置**:在代码中硬编码了Reactor Netty的特定实现 #### 解决方案步骤 1. **检查并修正依赖配置**(关键步骤) ```xml <!-- Maven 配置示例 --> <dependencies> <!-- 正确:使用WebFlux starter会自动排除Tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- 错误:避免同时引入Servlet容器依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> <!-- 若必须使用Tomcat则设为provided --> </dependency> </dependencies> ``` - 确保`spring-boot-starter-webflux`是唯一Web框架依赖 - 通过`mvn dependency:tree`检查是否意外引入了Servlet API 2. **配置WebSocket升级策略**(代码调整) ```java @Configuration public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // 使用自动适配的策略而非硬编码ReactorNetty registry.addEndpoint("/ws-endpoint") .setHandshakeHandler(new DefaultHandshakeHandler()); } } ``` 3. **服务器环境适配** - **方案A(推荐)**:改用Netty服务器 ```java // application.properties spring.main.web-application-type=reactive ``` - **方案B**:强制Tomcat使用适配层 ```java @Bean public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() { return factory -> factory.addContextCustomizers(context -> { context.addServletContainerInitializer(new WebFluxContainerInitializer(), null); }); } ``` 4. **检查部署配置** - WAR部署时需确保`SpringApplicationBuilder.sources(Application.class)`加载正确 - 嵌入式Tomcat需添加适配器: ```java @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } } ``` #### 根本原因分析 该异常的本质是**运行环境与API不匹配**: - `ResponseFacade`属于Tomcat的Servlet API实现 - `HttpServerResponse`是Reactor Netty的响应对象 - 当Spring尝试在Tomcat环境中使用Netty专属的`ReactorNettyRequestUpgradeStrategy`时,类型转换必然失败 > **最佳实践**:在WebFlux应用中保持技术栈一致性,避免混合使用Servlet容器和Reactive组件。若需Tomcat支持,应使用`spring-boot-starter-webflux`的Servlet适配层而非直接依赖Netty API[^3][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值