E. Binary Numbers AND Sum--Codeforces Round #515 (Div. 3)--【前缀和】

版权声明: https://blog.csdn.net/qq_40791842/article/details/83043878

                                 E. Binary Numbers AND Sum

                                                                       time limit per test:1 second

                                                                       memory limit per test:256 megabytes

                                                                       input:standard input

                                                                       output:standard output

You are given two huge binary integer numbers aa and bb of lengths nn and mm respectively. You will repeat the following process: if b>0 , then add to the answer the value a & band divide b by 2 rounding down (i.e. remove the last digit of bb ), and repeat the process again, otherwise stop the process.

The value a & b means bitwise AND of a and b . Your task is to calculate the answer modulo 998244353 .

Note that you should add the value a & b to the answer in decimal notation, not in binary. So your task is to calculate the answer in decimal notation. For example, if a=10102 (1010) and b=10002 (810) , then the value a & bwill be equal to 8 , not to 1000.

Input

The first line of the input contains two integers n and m (1≤n,m≤200000 ) — the length of aa and the length of bb correspondingly.

The second line of the input contains one huge integer aa . It is guaranteed that this number consists of exactly nn zeroes and ones and the first digit is always 1 .

The third line of the input contains one huge integer b . It is guaranteed that this number consists of exactly mm zeroes and ones and the first digit is always 1 .

Output

Print the answer to this problem in decimal notation modulo 998244353 .

Examples

Input

4 4
1010
1101

Output

12

Input

4 5
1001
10101

Output

11

Note

The algorithm for the first example:

  1. add to the answer 10102 & 11012=10002=810and set b:=110 ;
  2. add to the answer 10102 & 1102=102=210 and set b:=11 ;
  3. add to the answer 10102 & 112=102=210and set b:=1 ;
  4. add to the answer 10102 & 12=02=010 and set b:=0 .

So the answer is 8+2+2+0=128+2+2+0=12 .

The algorithm for the second example:

  1. add to the answer 10012 & 101012=12=110and set b:=1010b:=1010 ;
  2. add to the answer 10012 & 10102=10002=810and set b:=101;
  3. add to the answer 10012 & 1012=12=110and set b:=10 ;
  4. add to the answer 10012 & 102=02=010and set b:=1;
  5. add to the answer 10012 & 12=12=110and set b:=0.

So the answer is 1+8+1+0+1=111+8+1+0+1=11 .

题意:给定另个特别大的二进制数a,b,每次将a&b加入到结果中,并且将b除以二,直到b==0;求该结果;

解法:首先除以二操作相当于去掉二进制数b的最后一位数;其次如果每除以二后直接算a&b时间明显不够,如果开始将a,b的最后一位对齐放置,将b除以二相当于将b右移一位,超出的位忽略掉,那么一共右移m次,在这个过程中,a中的第i位(从左至右数)与b中的前max(0,m-(n-i))个数依次对齐一次,所以如果a[i]==1,计算b的前max(m-(n-i))个数中有多少个1就可以了(前缀和维护),然后乘以1<<(n-i),注意结果% 998244353;

附代码:

#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define PER(i,x) for(auto i=x.begin();i!=x.end();i++)
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
typedef long long ll;
const double eps=1.0e-5;
const int maxn=200000+10;
const long long mod=998244353;

int a[maxn],b[maxn],sum[maxn],n,m;
char s[maxn];

int main()
{
	scanf("%d%d",&n,&m);
	scanf("%s",s+1);
	per(i,1,n) a[i]=(int)(s[i]-'0');
	scanf("%s",s+1);
	per(i,1,m) b[i]=(int)(s[i]-'0');
	sum[0]=0;
	per(i,1,m) sum[i]=sum[i-1]+b[i];
	
	int cur=1;
	ll ans=0;
	rep(i,n,1){
		if(a[i]==1) ans=(ans+((ll)sum[max(0,m-n+i)]*cur%mod))%mod;
		cur=(2*cur)%mod;
	}
	
	printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/qq_40791842/article/details/83043878