SDUT3146:Integer division 2(整数划分区间dp)
题目:传送门
题目描述
This is a very simple problem, just like previous one.
You are given a postive integer n, and you need to divide this integer into m pieces. Then multiply the m pieces together. There are many way to do this. But shadow95 want to know the maximum result you can get.
输入
First line is a postive integer t, means there are T test cases.
Following T lines, each line there are two integer n, m. (0<=n<=10^18, 0 < m < log10(n))
输出
Output the maximum result you can get.
示例输入
1 123 2
示例输出
36
提示
You can divide "123" to "12" and "3".
Then the maximum result is 12*3=36.
题意很简单,但是我就是个渣渣,dp的题在比赛里从来没有A过,果断还是看了题解,也只是会了这个类型,其他类型的区间dp果断还是不会,果断不能举一反三啊。
代码如下:
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> typedef long long ll; using namespace std; #define mod 1000000007 int m,l; char s[22];//局部变量与全局变量求字符串长度完全不同 ll a[20][20],dp[20][20]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%s",s+1); scanf("%d",&m); l=strlen(s+1); memset(a,0,sizeof(a)); if(m==1||m==0) { printf("%s\n",s+1); continue; } for(int i=1;i<=l;i++) { for(int j=i;j<=l;j++) { a[i][j]=a[i][j-1]*10+(s[j]-'0'); } } memset(dp,0,sizeof(dp)); for(int i=0;i<=l;i++) dp[i][1]=a[1][i]; for(int j=2;j<=m;j++) { for(int i=j;i<=l;i++) { for(int k=1;k<i;k++) { dp[i][j]=max(dp[i][j],dp[k][j-1]*a[k+1][i]); } } } printf("%lld\n",dp[l][m]); } return 0; }
分类:
数据结构
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构