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.
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.
Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut.
3 0 1 0
1
5 1 0 1 0 1
4
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
题意:给定n个0-1数字,要求你划分若干个连续的块使得每个块有且只有一个1,问有多少种方案数。
思路:设置dp[i]为前i为数字的方案数。
a[i] == 0, dp[i] = dp[i-1];
a[i] != 0,dp[i] = dp[i-1] * (cnt+1),cnt为相邻两个1之间0的个数。
注意去掉前面的0就好了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (1000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
LL dp[110];
int a[110];
int main()
{
int n; Ri(n); int i;
for(i = 1; i <= n; i++)
Ri(a[i]);
for(i = 1; i <= n; i++)
if(a[i]) break;
int cnt = 0; dp[i] = 1LL;
i++;
for(; i <= n; i++)
{
if(a[i] == 0)
{
cnt++;
dp[i] = dp[i-1];
}
else
dp[i] = dp[i-1] * (cnt+1), cnt = 0;
}
Pl(dp[n]);
return 0;
}