Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
1s | 8192K | 444 | 140 | Standard |
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)
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.
For each case,just output the max product of the m smaller fingers in a line.
12345 2 12345 3
6170 2460
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