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

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

Input

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

Examples

Input

4 4
1010
1101

Output

12

Input

4 5
1001
10101

Output

11

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

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

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值