codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Vasily the Bear loves beautiful strings. String s is beautiful if it meets the following criteria:
- String s only consists of characters 0 and 1, at that character 0 must occur in string s exactly n times, and character 1 must occur exactly m times.
- We can obtain character g from string s with some (possibly, zero) number of modifications. The character g equals either zero or one.
A modification of string with length at least two is the following operation: we replace two last characters from the string by exactly one other character. This character equals one if it replaces two zeros, otherwise it equals zero. For example, one modification transforms string "01010" into string "0100", two modifications transform it to "011". It is forbidden to modify a string with length less than two.
Help the Bear, count the number of beautiful strings. As the number of beautiful strings can be rather large, print the remainder after dividing the number by 1000000007 (109 + 7).
The first line of the input contains three space-separated integers n, m, g (0 ≤ n, m ≤ 105, n + m ≥ 1, 0 ≤ g ≤ 1).
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
1 1 0
2
2 2 0
4
1 1 1
0
In the first sample the beautiful strings are: "01", "10".
In the second sample the beautiful strings are: "0011", "1001", "1010", "1100".
In the third sample there are no beautiful strings.
注意边界时候的特殊情况即可
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 200010
ll fac[];
const ll MOD = ;
void ext_gcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b){d=a,x=,y=;}
else{
ext_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
ll inv(ll a){
ll d,x,y;
ext_gcd(a,MOD,d,x,y);
return d== ? (x+MOD)%MOD : -;
}
ll C(int a,int b){
ll ret=fac[a];
ret=ret*inv(fac[b])%MOD;
ret=ret*inv(fac[a-b])%MOD;
return ret;
}
int main()
{
ios::sync_with_stdio(false);
int n,m,g;
fac[]=;
for(int i=;i<MAXN;i++)fac[i]=fac[i-]*i%MOD;
cin>>n>>m>>g;
ll ans=;
if(!n){
if(m==)ans=;
else ans=;
if(g)ans=-ans;
cout<<ans<<endl;
}else if(m==){
if(n&)ans=;
else ans=;
if(g)ans=-ans;
cout<<ans<<endl;
}else{
ll tot=C(n+m,n);
ans=;
for(int i=;i<=n;i+=)
ans=(ans+C(n+m--i,m-))%MOD;
if(m==){
if(n&)ans++;
else ans--;
}
ans=(ans+MOD)%MOD;
if(g)ans=(tot-ans+MOD)%MOD;
cout<<ans<<endl;
}
return ;
}
codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)的更多相关文章
- 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, ...
- 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 ...
- Codeforces Round #195 (Div. 2) A. Vasily the Bear and Triangle
水题,注意数据范围即可 #include <iostream> #include <algorithm> #include <utility> using name ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
随机推荐
- JasperReport使用心得
1. JasperReport 报表文件视图化生成工具iReport. iReport做为一个生成JasperReport的视图工具,和我们是使用的大多数报表创建工具没有太大的差别,都是拖控件,搭出报 ...
- POJ 1001 Exponentiation 模拟小数幂
模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...
- hdu 2564 词组缩写
Problem Description 定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写. 比如,C语言里常用的EOF就是end of file的缩写. Input 输入的第一行是一个整数T ...
- otf VS ttf images
- Angry Professor
def main(): t = int(raw_input()) for _ in range(t): n, k = map(int, raw_input().strip().split(' ')) ...
- akoj-1048-求某一整数序列的全排列问题
求某一整数序列的全排列问题 Time Limit:1000MS Memory Limit:65536K Total Submit:35 Accepted:16 Description 现有一整数序列 ...
- MvcPager概述
MvcPager 概述 MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottG ...
- mongodb 基本语法
成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...
- stm32通用定时器中断问题
在使用stm32的通用定时器定时中断的时候,发现定时器在完成初始化配置后,定时器UIF位会立刻置位,导致在使能中断后,程序会立刻进入定时器中断. 如果设计代码时不希望定时器配置完成后,立刻进入中断,可 ...
- 案例:用JS实现放大镜特效
案例:用JS实现放大镜特效 案例:用JS实现放大镜特效