Codeforces Round #340 (Div. 2) B. Chocolate 组合学、简单题

来源:互联网 发布:单片机 多少位 编辑:程序博客网 时间:2024/05/17 18:18

B. Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
30 1 0
output
1
input
51 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


Source

Codeforces Round #340 (Div. 2)


My Solution

0 1 0 1 0 1 0

从左向右遍历, 遇到第一个1后开始算(ans = 1), 然后连续的cnt个0 碰到隔断连续0的1的时候 ans *= cnt, cnt = 0, 然后继续访问

这样就把左端的连续0和右端的连续0去掉了

2 发 Wrong answer on test 7

尴尬 最开始的时候只包括了 0 1 0 1 0, 1 0 1, 1 1 1 1 1 1这些情况,

但漏了0 0 0 0 0 0,

所以初始化为 ans = 0; // 最开始的时候是初始化为 1 的

复杂度 O(n)


#include <iostream>#include <cstdio>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;int main(){    #ifdef LOCAL    freopen("b.txt", "r", stdin);    //freopen("o.txt", "w", stdout);    int T = 2;    while(T--){    #endif // LOCAL    ios::sync_with_stdio(false);    cin.tie(0);    int n, val;    LL cnt = 0, ans = 0;    bool flag = true;    cin>>n;    for(int i = 0; i < n; i++){        cin>>val;        if(flag){            if(val == 1) {flag = false; ans = 1;}            else continue;        }        if(val == 0){            cnt++;        }        else if(cnt != 0){   //末尾是 0 0 不要紧            ans *= (cnt + 1);            cnt = 0;        }    }    //if(cnt1 == 1) cout<<1<<endl;    //else    cout<<ans;    #ifdef LOCAL    cout<<endl;    }    #endif // LOCAL    return 0;}

  Thank you!

                                                                                                                                               ------from ProLights

0 0
原创粉丝点击