题意:

给出n,m,g,求好串的个数

0 <= n,m <= 10^5,n + m >= 1,0 <= g <= 1

好串的定义:

1.只由0,1组成,并且恰好有n个0,m个1

2.串的value = g

串的value的计算方式:

每次将最后2个字符替换,直至串的长度为1,该字符就是串的value

00 -> 1,   01,11,10 -> 0

solution:

首先,总方案数 = C(n + m, m)

m = 0时,特殊判断

设f(n,m)为n个0,m个1时,value为1的方案数

g(n,m)为n个0,m个1时,value为0的方案数

则f(n,m) + g(n,m) = C(n+m,m)

观察1个长度 > 1的串,若该串的value = 1

则str[1] = 0,且value(str[2] ~ str[n]) = 0

则有f(n,m) = g(n-1,m) = C(n-1+m,m) - f(n-1,m)

注意到,f的值与m无关(m固定后)

则设f(i) 为有i个0,m个1时,value = 1的方案数

则有f(i+1) = C(i+m,m) - f(i)

init: m=1,f(0) = 1

  m>1,f(0) = 0

g = 1,ans = f(n)

g = 0,ans = C(n+m,m) - f(n)

  //File Name: cf336D.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年02月17日 星期三 20时38分47秒 #include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <algorithm> #define LL long long using namespace std; const int MAXN = 1e5+;
const int MOD = 1e9+; LL f[MAXN];
LL jie[MAXN << ]; LL qp(LL x,LL y)
{
LL res = 1LL;
while(y){
if(y & )
res = res * x % MOD;
x = x * x % MOD;
y >>= ;
}
return res;
} LL comb(int x,int y)
{
if(y < || y > x)
return ;
if(y == || y == x)
return ;
return jie[x] * qp(jie[y] * jie[x - y] % MOD,MOD - ) % MOD;
} void init()
{
jie[] = ;
for(int i=;i<MAXN * ;i++){
jie[i] = jie[i-] * i % MOD;
}
} void solve(int n,int m,int g)
{
if(m == ){
int num_0 = ,num_1 = ;
if(n % )
num_0 = ;
else
num_1 = ;
printf("%d\n",g ? num_1:num_0);
return ;
}
init();
memset(f,,sizeof f);
f[] = (m == ? : );
for(int i=;i<n;i++){
f[i+] =((comb(i + m, i) - f[i] + MOD ) % MOD + MOD) % MOD;
}
LL ans = f[n];
if(!g)
ans = ((comb(n+m,n) - f[n] + MOD) % MOD + MOD) % MOD;
printf("%d\n",(int)ans);
return ;
} int main()
{
int n,m,g;
while(~scanf("%d %d %d",&n,&m,&g)){
solve(n,m,g);
}
return ;
}

codeforces 336D. Vasily the Bear and Beautiful Strings 组合数学 dp的更多相关文章

  1. codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vas ...

  2. Codeforces Round #195 (Div. 2) D题Vasily the Bear and Beautiful Strings

    这场CF,脑子乱死啊...C题,搞了很长时间,结束了,才想到怎么做.B题,没看,D题,今天看了一下,很不错的组合题. 如果n和m都挺多的时候 以下情况都是变为1,根据偶数个0,最后将会为1,奇数个0, ...

  3. codeforces 336C Vasily the Bear and Sequence(贪心)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Sequence Vasily the b ...

  4. codeforces C. Vasily the Bear and Sequence 解题报告

    题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...

  5. codeforces A. Vasily the Bear and Triangle 解题报告

    题目链接:http://codeforces.com/problemset/problem/336/A 好简单的一条数学题,是8月9日的.比赛中没有做出来,今天看,从pupil变成Newbie了,那个 ...

  6. C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)

    C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...

  7. Codeforces Round #604 (Div. 2) A. Beautiful String

    链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...

  8. CF643E. Bear and Destroying Subtrees 期望dp

    题目链接 CF643E. Bear and Destroying Subtrees 题解 dp[i][j]表示以i为根的子树中,树高小于等于j的概率 转移就是dp[i][j] = 0.5 + 0.5 ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. 为什么你应该试试用Sublog写博客

    HI 这篇文章发布后,收到了一些反馈,在不同的阅读媒体上(浏览器,RSS,evernote)等,会有样式兼容问题,特别是之前的代码显示行号的实现方式,使用浮动code块,兼容问题比较严重,所以做了一个 ...

  2. python设置字体颜色

    在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想要的信 ...

  3. POI读取excel

    HSSF是Horrible Spread Sheet Format的缩写 读取2007版本前 XSSF是XML Spread Sheet Format的缩写 读取2007版本后(包含2007)

  4. Java 标准日志工具 Log4j 的使用(附源代码)

    源代码下载 Log4j 是事实上的 Java 标准日志工具.会不会用 Log4j 在一定程度上可以说是衡量一个开发人员是否是一位合格的 Java 程序员的标准.如果你是一名 Java 程序员,如果你还 ...

  5. PHP-FPM-failed to ptrace(PEEKDATA) pid 123: Input/output error

    If you're running PHP-FPM you can see these kind of errors in your PHP-FPM logs. $ tail -f php-fpm.l ...

  6. Win7+VS2010环境下CEGUI 0.8.4编译过程详解

    转载▼   1. 在http://cegui.org.uk/download 下载CEGUI源码包 cegui-0.8.4 以及CEGUI依赖库(Windows / Apple OS X only) ...

  7. sublime安装sftp和ctags插件

    1. 安装Package Control插件 , 安装是通过Sublime Text 2控制台.这是通过按Ctrl + `快捷访问.一旦打开,粘贴以下命令到控制台. 输入以下python代码 subl ...

  8. 怎么用OCR图文识别软件在MS Office中创建PDF文件

    ABBYY PDF Transformer+是一款可创建.编辑及将PDF文件转换为其他可编辑格式的OCR图文识别软件,不仅可以从纸质文档.图像文件和任何其他流行格式创建PDF文件(相关文章请参考如何从 ...

  9. HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效。

    HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息模块 IIS Web Core 通知 BeginReques ...

  10. 017. ADO.NET Connection和command及DataReader

    ADO.NET主要包括Connection , command , DataReader, DataSet, DataAdapter5个对象, 通过这5个对象可以对数据库进行查询, 添加, 修改及删除 ...