[codeforces1066E]Binary Numbers AND Sum
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;
}