zoukankan      html  css  js  c++  java
  • codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

    转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

    Vasily the Bear and Beautiful Strings

    Vasily the Bear loves beautiful strings. String s is beautiful if it meets the following criteria:

    1. 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.
    2. We can obtain character g from string s with some (possibly, zero) number of modifications. The character g equals either zero or one.

    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).

    Input

    The first line of the input contains three space-separated integers n, m, g (0 ≤ n, m ≤ 105, n + m ≥ 1, 0 ≤ g ≤ 1).

    Output

    Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

    Sample test(s)
    input
    1 1 0
    output
    2
    input
    2 2 0
    output
    4
    input
    1 1 1
    output
    0
    Note

    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.

    注意边界时候的特殊情况即可

     1 //#####################
     2 //Author:fraud
     3 //Blog: http://www.cnblogs.com/fraud/
     4 //#####################
     5 #include <iostream>
     6 #include <sstream>
     7 #include <ios>
     8 #include <iomanip>
     9 #include <functional>
    10 #include <algorithm>
    11 #include <vector>
    12 #include <string>
    13 #include <list>
    14 #include <queue>
    15 #include <deque>
    16 #include <stack>
    17 #include <set>
    18 #include <map>
    19 #include <cstdio>
    20 #include <cstdlib>
    21 #include <cmath>
    22 #include <cstring>
    23 #include <climits>
    24 #include <cctype>
    25 using namespace std;
    26 #define XINF INT_MAX
    27 #define INF 0x3FFFFFFF
    28 #define MP(X,Y) make_pair(X,Y)
    29 #define PB(X) push_back(X)
    30 #define REP(X,N) for(int X=0;X<N;X++)
    31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
    32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
    33 #define CLR(A,X) memset(A,X,sizeof(A))
    34 #define IT iterator
    35 typedef long long ll;
    36 typedef pair<int,int> PII;
    37 typedef vector<PII> VII;
    38 typedef vector<int> VI;
    39 #define MAXN 200010
    40 ll fac[200010];
    41 const ll MOD = 1000000007;
    42 void ext_gcd(ll a,ll b,ll &d,ll &x,ll &y){
    43     if(!b){d=a,x=1,y=0;}
    44     else{
    45         ext_gcd(b,a%b,d,y,x);
    46         y-=x*(a/b);
    47     }
    48 }
    49 ll inv(ll a){
    50     ll d,x,y;
    51     ext_gcd(a,MOD,d,x,y);
    52     return d==1 ? (x+MOD)%MOD : -1;
    53 }
    54 ll C(int a,int b){
    55     ll ret=fac[a];
    56     ret=ret*inv(fac[b])%MOD;
    57     ret=ret*inv(fac[a-b])%MOD;
    58     return ret;
    59 }
    60 int main()
    61 {
    62     ios::sync_with_stdio(false);
    63     int n,m,g;
    64     fac[0]=1;
    65     for(int i=1;i<MAXN;i++)fac[i]=fac[i-1]*i%MOD;
    66     cin>>n>>m>>g;
    67     ll ans=0;
    68     if(!n){
    69         if(m==1)ans=0;
    70         else ans=1;
    71         if(g)ans=1-ans;
    72         cout<<ans<<endl;
    73     }else if(m==0){
    74         if(n&1)ans=1;
    75         else ans=0;
    76         if(g)ans=1-ans;
    77         cout<<ans<<endl;
    78     }else{
    79         ll tot=C(n+m,n);
    80         ans=0;
    81         for(int i=0;i<=n;i+=2)
    82             ans=(ans+C(n+m-1-i,m-1))%MOD;
    83         if(m==1){
    84             if(n&1)ans++;
    85             else ans--;
    86         }
    87         ans=(ans+MOD)%MOD;
    88         if(g)ans=(tot-ans+MOD)%MOD;
    89         cout<<ans<<endl;
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    CSS 选择器及各样式引用方式
    The usage of docker image wurstmeister/kafka
    kafka connect rest api
    Kafka connect in practice(3): distributed mode mysql binlog ->kafka->hive
    jail-break-rule
    Ubuntu下把缺省的dash shell修改为bash shell
    mysql 5.7 enable binlog
    Docker CMD in detail
    记一次深度系统安装至windows系统盘提示挂载为只读模式问题
    android 监听声音变化
  • 原文地址:https://www.cnblogs.com/fraud/p/4397186.html
Copyright © 2011-2022 走看看