CodeForces -336D. Vasily the Bear and Beautiful Strings (combinatorial mathematics)

Topic link

Question meaning:
Now if you give a string S that is a string contained by 01, change this string to the meaning of the question, every time you take the last two characters, if it is "00", it becomes "1" and added to it At the end, if it is "01" or "10" or "11", it becomes "0" instead of adding it to the end, repeating until there is only one character left in the string

Now give n, m, g (0 <= n, m <= 10^5, n + m >= 1, g == 0 || g == 1)

Find out if a string originally has n 0s and m 1s, and the character obtained after the appeal change is g, how many possibilities are there in the original string, and the final result is 10^9 + 7 modulo output
solution:
first We see that this question must be able to get such a conclusion
(the number of options for g=0) + (the number of options for g=1) = C(n+m, m);
so no matter what he asks us to find (the number of options for g=0) Number) or (the number of schemes with g=1), we can convert it to (the number of schemes with g=0). Why should it be converted to (g=0)? Look down.
Analyze the meaning of the question: we know that in addition to 00 000 0 Generate1 11 , everything else generates0 00 . So we know that1 11 No matter what you put together, it must be0 00 So, so as long asthe string at the beginning of 1must be0. Then we can putthe string starting with 1after them onthe basis of determining the even number of 0s. Itmust be 0, because we think of the odd number of 0s.
In fact, this is the form

00001...... 这是确定了4个0和1个1,所以(n+m-4-1)这个么多数组合就行了也就是C(n+m-4-1,m-1);

We also need to judge a case where m=1 means there is only one 1.

  1. If n is an odd number, then we missed a case where 1 is at the top, followed by an odd number of 0s.
  2. If n is an even number, then we add one more, n 0+1 00001cases, this result is 1.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+7;
const int mod=1e9+7;
ll n,m,cnt;
ll fac[maxn],infac[maxn];
ll qpow(ll a,ll b) {
    
    
    ll sum=1;
    while(b) {
    
    
        if(b&1) sum=sum*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return sum;
}
void init() {
    
    
    fac[0]=fac[1]=1;
    for(int i=2; i<=200000; i++) {
    
    
        fac[i]=fac[i-1]*i%mod;
    }
    for(int i=200000; i>=0; i--) {
    
    
        infac[i]=qpow(fac[i],mod-2);
    }
}
ll C(ll n,ll m) {
    
    
    return fac[n]*infac[m]%mod*infac[n-m]%mod;
}


int main() {
    
    
    ll g;
    cin>>n>>m>>g;
    if(n==0) {
    
     ///没有0 除了一个1之外不能构成1
        if(g==0) {
    
    
            if(m==1) printf("0\n");
            else puts("1");
        } else {
    
    
            if(m==1) puts("1");
            else puts("0");
        }
        return 0;
    }
    if(m==0) {
    
     ///没有1 
        if(g==1) {
    
     ///奇数个0可以构成1
            if(n&1)printf("0\n");
            else puts("1");
        } else {
    
    
            if(n&1)puts("1");
            else puts("0");
        }
        return 0;
    }
    init();
    ll all=C(n+m,m);
    ll ans=0;///0的方案数
    for(int i=0; i<=n; i+=2) {
    
    
        ans=(ans+C(n-1-i+m,m-1))%mod;///除去确定的偶数个0和一个1
    }
    if(m==1&&(n&1)==0) {
    
    
        ans=(ans-1+mod)%mod;
    }
    if(m==1&&(n&1)) {
    
    
        ans=(ans+1)%mod;
    }
    if(g==0) printf("%lld\n",ans);
    else printf("%lld\n",(all-ans+mod)%mod);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/110730688