E. Binary Numbers AND Sum (Thinking + Prefix)

https://codeforces.com/problemset/problem/1066/E


Idea: In the process of shifting the lower string to the right, you can calculate the contribution of each 1 of the lower string and the 1& of the upper string. You can run the prefix sum of the upper string on On, and the answer is the accumulation.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<deque>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=998244353;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL suf[maxn];
deque<char>s,p;
LL ksm(LL a,LL k){
   LL res=1;
   while(k>0){
    if(k&1) res=res*a%mod;
    k>>=1;
    a=a*a%mod;
   }
   return res%mod;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++) {char op;cin>>op;s.push_back(op);}
  for(LL i=1;i<=m;i++) {char op;cin>>op;p.push_back(op);}
  LL cnt=abs(n-m);
  if(n<m){
    for(LL i=1;i<=cnt;i++) s.push_front('0');
  }
  else if(n>m){
    for(LL i=1;i<=cnt;i++) p.push_front('0');
  }

  LL ans=0;cnt=0;
  for(LL i=max(n,m);i>=1;i--){
     char now=s.back();s.pop_back();
     suf[i]=suf[i+1];
     if(now=='1') suf[i]=(suf[i]%mod+ksm(2,cnt%mod)%mod)%mod;
     char down=p.back();p.pop_back();
     if(down=='1'){
        ans=(ans%mod+suf[i]%mod)%mod;
     }
     cnt++;
  }
  cout<<ans%mod<<"\n";
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/115189374