Codeforces Round #340 (Div. 2) B. Chocolate 组合学、简单题
来源:互联网 发布:单片机 多少位 编辑:程序博客网 时间:2024/05/17 18:18
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.
30 1 0
1
51 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
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
- Codeforces Round #340 (Div. 2) B. Chocolate 组合学、简单题
- Codeforces 617B Chocolate (Round #340 (Div. 2) B题)
- Codeforces Round #340 (Div. 2):B. Chocolate
- Codeforces Round #340 (Div. 2)-B. Chocolate(组合数学的乘法计数)
- 【Codeforces Round 340 (Div 2)B】【简单题】Chocolate 巧克力划分方案数 每段恰有一个坚果
- Codeforces Round #279 (Div. 2) D. Chocolate
- Codeforces Round #279 (Div. 2) D. Chocolate
- Codeforces Round #279 (Div. 2) D. Chocolate
- Codeforces Round #279 (Div. 2)D. Chocolate
- Codeforces Round #340-B. Chocolate-水+坑
- (组合)Codeforces AIM Tech Round 4 (Div. 2) B. Rectangles
- Codeforces Round #169 (Div. 2) B题
- Codeforces Round #188 (Div. 2) B题
- Codeforces Round #218 (Div. 2) B题
- Codeforces Round #238 (Div. 2) B题
- Codeforces Round #402 (Div. 2) B题
- Codeforces Round #404 (Div. 2) B题
- Codeforces Round #446 (Div. 2) B 题
- mysql 给表添加一个联合唯一索引
- 约瑟夫问题(循环链表实现)
- 距离向量路由算法的Java模拟
- 双系统ubuntu14.04无法引导win10问题
- LinkedList
- Codeforces Round #340 (Div. 2) B. Chocolate 组合学、简单题
- 实现一键finish所有打开的Activity
- VS CodeSnippet
- leetCode No.238 Product of Array Except Self
- SSM(五)基于webSocket的聊天室
- 在SpingMVC的Interceptor中如何得到被调用方法名
- 【转】Java线程池的那些事--线程计算公式
- 可重入函数
- 入门级CUDA程序调试的通用方法——可用于Matlab的MexCuda