您的位置:首页 > 其它

CodeForces 617 B. Chocolate(组合数学)

2016-02-26 10:59 387 查看
Description

给出一个只由01组成的序列,要求将这个序列分成若干段,每段仅含一个1,问有多少种划分方法

Input

第一行为一整数n表示序列长度,之后n个整数表示该序列(1<=n<=100)

Output

输出划分方案数

Sample Input

5

1 0 1 0 1

Sample Output

4

Solution

简单计数,记录每个1的位置pos[i],假设有res个1,那么ans=(pos[2]-pos[1])(pos[3]-pos[2])…*(pos[res]-pos[res-1])

Code

#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 111
int n,a[maxn],res,pos[maxn];
long long ans;
int main()
{
while(~scanf("%d",&n))
{
res=0;ans=1ll;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)pos[++res]=i;
}
if(res==0)ans=0;
for(int i=1;i<res;i++)
ans*=(pos[i+1]-pos[i]);
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: