codeforces 336D. Vasily the Bear and Beautiful Strings 组合数学 dp
题意:
给出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的更多相关文章
- codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Beautiful Strings Vas ...
- Codeforces Round #195 (Div. 2) D题Vasily the Bear and Beautiful Strings
这场CF,脑子乱死啊...C题,搞了很长时间,结束了,才想到怎么做.B题,没看,D题,今天看了一下,很不错的组合题. 如果n和m都挺多的时候 以下情况都是变为1,根据偶数个0,最后将会为1,奇数个0, ...
- codeforces 336C Vasily the Bear and Sequence(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Sequence Vasily the b ...
- codeforces C. Vasily the Bear and Sequence 解题报告
题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...
- codeforces A. Vasily the Bear and Triangle 解题报告
题目链接:http://codeforces.com/problemset/problem/336/A 好简单的一条数学题,是8月9日的.比赛中没有做出来,今天看,从pupil变成Newbie了,那个 ...
- 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 ...
- Codeforces Round #604 (Div. 2) A. Beautiful String
链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...
- CF643E. Bear and Destroying Subtrees 期望dp
题目链接 CF643E. Bear and Destroying Subtrees 题解 dp[i][j]表示以i为根的子树中,树高小于等于j的概率 转移就是dp[i][j] = 0.5 + 0.5 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
随机推荐
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- 比较实用的JavaScript库
如果你操作过cookie的接口,那么你一定会感觉这东西的规范真的是太复杂了,根本记不住啊,其实你是对的,因为cookie的接口设计的是有问题的,也就是说设计的太底层了,根本不友好,那么来试试这个js库 ...
- 对于python,一切事物都是对象,对象基于类创建
新建列表.新建string字符串 li1 = [1, 2, 3, 4] li2 = list([1, 2, 3]) s1 = "abc" s2 = str("abc&qu ...
- OpenJudge计算概论-单词替换
/*====================================================================== 单词替换 总时间限制: 1000ms 内存限制: 65 ...
- 【转】WMI使用的WIN32_类库名
ShadowBy--Win32_ShadowContext--Win32_ShadowCopy--Win32_ShadowDiffVolumeSupport--Win32_ShadowFor--Win ...
- 一种JavaScript 类的设计模式
一种JavaScript 类的设计模式尽管前面介绍了如何定义一个类,如何初始化一个类的实例,但既可以在function定义的函数体中添加成员,又可以用prototype 定义类的成员,代码显的很混乱, ...
- ExtJs学习笔记之Window组件
Window窗体组件 window是一个指定的打算作为一个应用程序窗口的面板,默认窗口是浮动的,resizable, 并且draggable,默认的,窗体靠document.body呈现. 1.示例: ...
- android数据存储之File
android中使用File进行存储主要使用到OpenFileOutput和OpenFileInput两个方法,下面直接用一个例子来说明一下. (1)布局文件main.xml文件 <?xml v ...
- SourceTree克隆仓库时,总是提示输入密码
1.SourceTree的“工具”-“选项”-“一般”,配置上SSH秘钥: 2.将SSH密钥,配置到GitLab里: 3.仓库的“源路径 / URL”,填写SSH地址:
- Apache 性能优化
有一个升级服务器,这几天一直访问的比较慢.导致部分用户升级不了.看了一下服务器的负载,发现 CPU和内存占用的都不是很高,可能是Apache配置不当造成的,一番搜索,找到了MPM的配置,提速很明显哦 ...