Description
Bob loves everything sweet. His favorite chocolate 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.
Example
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
题意
将一块巧克力分成许多块,问有多少种分法。分法是每一块中可以单独有1,但是不能单独有0,保证分成的每块都有1
分析:
有点类似于排列组合中的隔板法,首先根据给出的例子:
1 0 1 0 1
要将三个1分开,将0插入其中,用隔板法将其分隔。
每两个1之间的0进行分隔,可知,前两个1之间的0可以分割给第一个1也可以分割给第二个1,所以有两种分法1 2 ;同理,第二个1与第三个1之间的0也有两种分法a b。这两个0之间互不影响,所以每种分法都可以组合,即法1可以与a 也可以与 b,法2可以与a 也可以与 b,所以方法一共有2*2种。所以可得结论,就是两个1之间相隔数的垒乘。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max 200
using namespace std;
int n;
int a[Max];
int p[Max];
//0表示不带螺母,1表示带螺母
int main()
{
while(scanf("%d",&n)!=EOF)
{
int k = 0;
long long sum = 1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i] == 1) //luo mu
{
p[k] = i; //记录1的位置,方便看两个1之间0的个数
k++;
}
}
if(k == 0)
printf("0\n");
else
{
for(int i=1;i<k;i++)
{
sum = sum * (p[i]-p[i-1]); //两个1之间相差几个0
}
printf("%lld\n",sum);
}
}
return 0;
}