题意:

给出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. URAL 1076 Trash Trash(最大权匹配)

    Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...

  2. nginx下的rewrite

    一.正则表达式匹配,其中: * ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 二.文件及目录匹配,其中: * -f和!-f用来判断是否 ...

  3. STL学习小结

    STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合,这些" ...

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

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

  5. HashMap的原理与实 无锁队列的实现Java HashMap的死循环 red black tree

    http://www.cnblogs.com/fornever/archive/2011/12/02/2270692.html https://zh.wikipedia.org/wiki/%E7%BA ...

  6. PADS Layout 使用

    1.设置板子中心 setup-set origin 1.画板子边框(Board Outline) Board outline and cutout umm设置单位毫米 g 10 设置间隔 右键设置方形 ...

  7. 【转】DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10

    [转]DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10 分类: Linux 由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至 ...

  8. 给OCR文字识别软件添加图像的方法

    ABBYY FineReader 12是一款OCR图片文字识别软件,而且强大的它现在还可使用快速扫描窗口中的快速打开.扫描并保存为图像或任务自动化任务,在没有进行预处理和OCR的ABBYY FineR ...

  9. SVG ViewBox

    如果svg图形太大或者太小,就可以用ViewBox属性来调整在页面中的显示范围.大小. "像素不能直接换算成英寸.厘米,要在 dpi ( dot per inch 分辨率,概念较多,鼠标 d ...

  10. 【转】ASP.NET的OnClientClick与OnClick事件【解决了“识别用户在对话框里面选yes或no的问题”】

    OnClientClick是客户端事件方法.一般采用JavaScript来进行处理.也就是直接在IE端运行.一点击就运行. OnClick事件是服务器端事件处理方法,在服务器端,也就是IIS中运行.点 ...