time limit per test : 1 second
memory limit per test : 256 megabytes
You are given two huge binary integer numbers aand b of lengths n and m respectively. You will repeat the following process: if , then add to the answer the value & and divide by rounding down (i.e. remove the last digit of ), and repeat the process again, otherwise stop the process.The value & means bitwise AND of and . Your task is to calculate the answer modulo
Note that you should add the value & to the answer in decimal notation, not in binary. So your task is to calculate the answer in decimal notation. For example, if and , then the value & will be equal to , not to
Input
The first line of the input contains two integers
and
— the length of
and the length of
correspondingly.
The second line of the input contains one huge integer
. It is guaranteed that this number consists of exactly n 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 m zeroes and ones and the first digit is always 1
Output
Print the answer to this problem in decimal notation modulo
Examples
Input
4 4
1010
1101
Output
12
Input
4 5
1001
10101
Output
11
题意:
给两个二进制数
,
, 位数分别为
,
,有下列操作:
step1.
&
step2.
step3. 如果
回到step1
求所有
的总和,由于总和可能会很大,所以总和对
取模
题解:
对于
来说,他处于从后往前数第
位上的
对答案的贡献,是
,记作
。
对于
来说,我们单独考虑他每个
对答案的贡献,是这个
所处的位置在
中所有能被他
到的
的贡献之和,这个对
处理一个后缀和就行了。
记作从第n位到第i位所有的
之和
#include<bits/stdc++.h>
#define LiangJiaJun main
#define MOD 998244353
using namespace std;
int bin[200004],n,m;
char a[200004],b[200004];
int cop[200004];
int w33ha(){
scanf("%s",a+1);
scanf("%s",b+1);
cop[n+1]=0;
for(int i=n;i>=1;i--){
if(a[i]=='0')cop[i]=cop[i+1];
else cop[i]=(cop[i+1]+bin[n-i])%MOD;
}
int ans=0;
for(int i=1;i<=m;i++){
if(b[i]=='0')continue;
int bg=m-i+1;
if(bg>n)bg=1;
else bg=n-bg+1;
ans=(ans+cop[bg])%MOD;
}
printf("%d\n",ans);
return 0;
}
int LiangJiaJun(){
bin[0]=1;
for(int i=1;i<=200001;i++)bin[i]=(bin[i-1]<<1)%MOD;
while(scanf("%d%d",&n,&m)!=EOF)w33ha();
return 0;
}