Chocolate
colate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces.
You are asked to calculate the number of ways he can do it. Two ways to break chocolate are considered distinct if one of them contains a break between some two adjacent pieces and the other one doesn't.
Please note, that if Bob doesn't make any breaks, all the bar will form one piece and it still has to have exactly one nut.
Input
The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of pieces in the chocolate bar.
The second line contains n integers ai (0 ≤ ai ≤ 1), where 0 represents a piece without the nut and 1 stands for a piece with the nut.
Output
Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut.
Sample test(s)
input
3 0 1 0
output
1
input
5 1 0 1 0 1
output
4
Note
In the first sample there is exactly one nut, so the number of ways equals 1 — Bob shouldn't make any breaks.
In the second sample you can break the bar in four ways:
10|10|1
1|010|1
10|1|01
1|01|01
---------------------editorial------------------------------------
this problem can easily solve if we use a dp with combinotorics
steps
start processing from left array if we are at the ith position of the array than consider our array is of length 0 to i only,
now sulppose our ith elsement is
case 1 -> ith element is 1 , than we can place a partition from i-1 till we get another 1 or end of the array (index -1),
so if we are placing a partition at index j <i than answer at ith index will be dp[i]+=dp[j] (summession of no of ways of arrangement of first j elements )
case 2-> ith element is 0 than we cant start placing partition until we gat a 1 and can place partition till next 1 , similarly crate dp for this also
final answer will in index dp[n]
--------------------------------------------code--------------------------------
#include<bits/stdc++.h>
typedef long long int lli;
typedef long int li;
typedef unsigned long long int ulli;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define test() int t, cin>>t,while(t--)
using namespace std;
long long int dp[200];
int main()
{
int n;
cin>>n;
int arr[200];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
dp[0]=1;
arr[0]=INT_MAX;
for(int i=1;i<=n;i++)
{
//cout<<" i "<<i<<endl;
int j=i-1;
if(arr[i]==1)
{
while( j>=0)
{
dp[i]+=dp[j];
if(j>=0 && arr[j]==1) break;
j--;
}
}
else
{
int f=0;
while(f<=1 && j>=0)
{
if(f==1)
{
dp[i]+=dp[j];
}
if(arr[j]==1) f++;
j--;
}
}
}
cout<<dp[n]<<endl;
}
No comments:
Post a Comment