joj2412

 2412: Divide the finger


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
1s8192K444140Standard

We can divide one figure into some smaller ones.For example,123456789 can be divided into 123,456,789,or 1,234567,89,which is similar to putting some boards between the numbers,while the order cannot be changed.Now the figure n is given and should be divided into m parts. Please work out the max product of these m smaller figures.(the first place of n is not 0)

Input

There are muliti test cases.Each case contains two integers n(1=< n < 1,000,000,000,000,000) and m(1=< m <=[lg(n)]),the number n and how many parts n should be divided to.

Output

For each case,just output the max product of the m smaller fingers in a line.

Sample Input

12345 2
12345 3

Sample Output

6170
2460

Hint

For case one,12345 can be divided as 1234|5,or 123|45,or ......
but the max product is 1234*5=6170.

Problem Source: provided by tank








#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
char str[20];
long long s[20][20][20];
long long convert(long long p1,long long p2)
{
    long long jishu=1;
    long long sum=0;
    while(p1<=p2)
    {
        sum+=jishu*(str[p2]-'0');
        p2--;
        jishu*=10;
    }
    return sum;
}
int main()
{
   // freopen("in.txt","r",stdin);
    int m;
    while(scanf("%s%d",str+1,&m)==2)
    {
        long long n=strlen(str+1);
        for(long long i=1;i<=n;i++)
        {
            for(long long j=i;j<=n;j++)
            {
                s[i][j][1]=convert(i,j);
            }
        }
        for(long long t=2;t<=m;t++)
        {
            for(long long i=1;i<=n;i++)
            {
                for(long long j=i+t-1;j<=n;j++)
                {
                    long long max=-1;
                    for(long long k=i;k+t-1<=j;k++)
                    {
                        if(max<s[i][k][1]*s[k+1][j][t-1])
                        {
                            max=s[i][k][1]*s[k+1][j][t-1];
                        }
                    }
                    //if(max>s[i][j][t])
                    s[i][j][t]=max;
                }
            }
        }
        cout<<s[1][n][m]<<endl;
    }
    return 0;
}


这个是一个动态规划的问题,通过s表示i到j,m个部分的最大值。。。




This problem is used for contest: 87  148 


Submit / Problem List / Status / Discuss

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值