2412: Divide the finger
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 1s | 8192K | 373 | 117 | 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)
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
This problem is used for contest: 87 148
#include<iostream>
#include<cstring>
using namespace std;
char num[100];
double w[100][100],maxn;
int n,len;
void dfs(int cur_pos,double sum,int total_pos)
{
int i;
if(total_pos==n) { if(sum>maxn) maxn=sum; return ;}
if(cur_pos>=len) return ;
for(i=cur_pos;i<len;i++) dfs(i+1,sum*w[cur_pos][i],total_pos+1);
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int i,j;
while(scanf("%s%d",num,&n)==2)
{
len=strlen(num);
for(i=0;i<len;i++)
{
w[i][i]=num[i]-'0';
for(j=i+1;j<len;j++)
{
w[i][j]=w[i][j-1]*10+num[j]-'0';
}
}
//for(i=0;i<len;i++) {for(j=0;j<len;j++) printf("%.0lf .",w[i][j]);printf("/n");}
maxn=0;
dfs(0,1,0);
printf("%.0lf/n",maxn);
}
return 0;
}