题意:

给出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. jQuery 1.10.3 参考手册

    Jquery是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+),jQuery2.0及后续版本将不再支持IE6/7 ...

  2. (转)The 9 Deep Learning Papers You Need To Know About (Understanding CNNs Part 3)

    Adit Deshpande CS Undergrad at UCLA ('19) Blog About The 9 Deep Learning Papers You Need To Know Abo ...

  3. 【Hadoop】搭建完全分布式的hadoop

    博客已转移,请借一步说话! http://www.weixuehao.com/archives/577 下面博文已更新,请移步 ↑ 用于测试,我用4台虚拟机搭建成了hadoop结构 我用了两个台式机. ...

  4. linux工具之log4j-LogBack-slf4j-commons-logging

    log4j http://commons.apache.org/proper/commons-logging/ http://logging.apache.org/log4j/2.x/ The Com ...

  5. 剑指offer(07)-调整数组顺序使奇数位于偶数前面【转】

    来源:http://www.acmerblog.com/offer-6-2429/ 题目来自剑指offer系列 九度 1516 题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得 ...

  6. OpenJudge计算概论-最长平台

    /*=========================================================== 最长平台 总时间限制: 1000ms 内存限制: 65536kB 描述 已知 ...

  7. pouchdb sync

    PouchDB and CouchDB were designed for one main purpose: sync. Jason Smith has a great quote about th ...

  8. Oracle报错,ORA-28001: 口令已经失效

    Oracle11G创建用户时缺省密码过期限制是180天(即6个月), 如果超过180天用户密码未做修改则该用户无法登录. Oracle公司是为了数据库的安全性默认在11G中引入了这个默认功能,但是这个 ...

  9. 编写使用SpringSecurity的JUnit测试提醒

    近日在使用SpringSecurity的项目中发现一个小问题,就是在接口上加了@Secured标注限制调用接口权限时,某些JUnit无法正常调用了. 例如: @Secured(PrivilegeDAO ...

  10. Linux dirname $0 source if

    $SHELL gives the full path to your default shell. $0 gives the name of your current shell. dirname是一 ...