[codeforces1066E]Binary Numbers AND Sum

版权声明:辛辛苦苦码字,你们转载的时候记得告诉我 https://blog.csdn.net/dxyinme/article/details/84107507

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 b > 0 b>0 , then add to the answer the value a a & b b and divide b b by 2 2 rounding down (i.e. remove the last digit of b b ), and repeat the process again, otherwise stop the process.The value a a & b b means bitwise AND of a a and b b . Your task is to calculate the answer modulo 998244353 998244353

Note that you should add the value a a & b 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 = 101 0 2 ( 1 0 10 ) a=1010_2 (10_{10}) and b = 100 0 2 ( 8 10 ) b=1000_2 (8_{10}) , then the value a a & b b will be equal to 8 8 , not to 1000 1000

Input

The first line of the input contains two integers n n and m ( 1 n , m 2 1 0 5 ) m (1≤n,m≤2⋅10^5) — the length of a a and the length of b b correspondingly.
The second line of the input contains one huge integer a a . 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 998244353 998244353

Examples

Input

4 4
1010
1101

Output

12

Input

4 5
1001
10101

Output

11

题意:
给两个二进制数 a a , b b , 位数分别为 n n , m ( 1 n , m 2 1 0 5 ) m (1≤n,m≤2⋅10^5) ,有下列操作:
step1. x : = x:= a a & b b
step2. b : = b / 2 b:=b/2
step3. 如果 b > 0 b>0 回到step1
求所有 x x 的总和,由于总和可能会很大,所以总和对 998244353 998244353 取模

题解:
对于 a a 来说,他处于从后往前数第 i i 位上的 1 1 对答案的贡献,是 1 < < i 1<<i ,记作 r e t i ret_i
对于 b b 来说,我们单独考虑他每个 1 1 对答案的贡献,是这个 1 1 所处的位置在 a a 中所有能被他 A N D AND 到的 1 1 的贡献之和,这个对 r e t i ret_i 处理一个后缀和就行了。
c o p i cop_i 记作从第n位到第i位所有的 r e t i ret_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;
}

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/84107507