2020 Hunan Programming Competition-C Problem Solution

Question: Give a 01 string, where are some positions? , Can be changed to 1 or 0. There is an operation
to subtract the absolute value of the adjacent positions of the 01 string into a new array. For example, 1 0 1 can become 1 1. The solution makes the 01 string value 1 after n-1 operations.
Solution:
The absolute value of the subtraction of adjacent positions is the XOR value of the two numbers. Finally, it can be found that the contribution of the i-th number to the final result is C(n-1,i-1) times, Therefore, when C(n-1,i-1) is an even number, the number in this position does not need to be considered, and vice versa. We can count how many question marks are in the non-contributing position, assuming there are k, these question marks It can be 1 or 0, then the number of solutions is 2^k, and then calculate how many question marks are contributing bits, and finally calculate how many 1s are needed for these question marks to make the number of solutions with the result 1 multiply the previous 2^ k is the answer.
Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+5,mod=1e9+7;
char s[maxn];
int vis[maxn];
ll fac[maxn];
void cal()
{
    
    
    fac[0]=1;
    for(int i=1; i<maxn; i++)
        fac[i]=fac[i-1]*i%mod;
}
ll qpow(ll a,ll b)
{
    
    
    ll ans=1;
    for(; b; b>>=1,a=a*a%mod)
        if(b&1)
            ans=ans*a%mod;
    return ans;
}

ll C(ll n,ll m)
{
    
    
    if(n<m)
        return 0;
    return fac[n]*qpow(fac[n-m]*fac[m]%mod,mod-2)%mod;
}
int main()
{
    
    
    cal();
    while(cin>>s+1)
    {
    
    
        int n=strlen(s+1);
        int ans=0;
        ll cnt=0;
        ll sum=1;
        for(int i=1;i<=n;i++)
        {
    
    
            int d=(n-1)&(i-1);
            if(d==i-1) vis[i]=1;
            else vis[i]=0;
            if(vis[i]==1)
            {
    
    
                if(s[i]=='1') ans^=1;
                else if(s[i]=='?') cnt++;
            }
            else
            {
    
    
                if(s[i]=='?') sum=sum*2%mod;
            }
        }
        
        ll ca=0;
        if(ans==1)
        {
    
    
            for(int i=0;i<=cnt;i+=2)
            {
    
    
                ca=(ca+C(cnt,i))%mod;
            }
        }
        else
        {
    
    
            for(int i=1;i<=cnt;i+=2)
            {
    
    
                ca=(ca+C(cnt,i))%mod;
            }
        }
        ll res=ca*sum%mod;
        cout<<res<<endl;
    }
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/109188524