cf1066E Binary Numbers AND Sum

题目:http://codeforces.com/contest/1066/problem/E

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>0b>0, then add to the answer the value a & ba & b and divide bb by 22 rounding down (i.e. remove the last digit of bb), and repeat the process again, otherwise stop the process.

The value a & ba & b means bitwise AND of aa and bb. Your task is to calculate the answer modulo 998244353998244353.

Note that you should add the value a & ba & 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)a=10102 (1010) and b=10002 (810)b=10002 (810), then the value a & ba & b will be equal to 88, not to 10001000.

Input

The first line of the input contains two integers nn and mm (1≤n,m≤2⋅1051≤n,m≤2⋅105) — 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 11.

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

Output

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

Examples

input

Copy

4 4
1010
1101

output

Copy

12

input

Copy

4 5
1001
10101

output

Copy

11

给出两个大数a和b,要求计算(a&b) + (a & (b >> 1)) + (a & (b >> 2)) + ...

如果只是计算a&b那么我们只需要把a和b的二进制存下来扫一遍就行了,现在多了几项东西,其实道理是一样的,我们看比这一位高的有多少个1,就会在这一位有多少贡献,前缀和维护一下。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
const ll mod = 998244353;
int abit[N];
int bbit[N];
char s[N];
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    memset(abit, 0, sizeof(abit));
    memset(bbit, 0, sizeof(bbit));
    scanf("%s", s);
    for (int i = 0; i < a; i++)
    {
        abit[a - i - 1] = s[i] - '0';
    }
    scanf("%s", s);
    for (int i = 0; i < b; i++)
    {
        bbit[b - i - 1] = s[i] - '0';
        if (i)
            bbit[b - i - 1] += bbit[b - i];
        //printf("%c %d\n", s[i], bbit[b - i - 1]);
    }
    ll power = 1;
    ll sum = 0;
    for (int i = 0; i <= max(a, b); i++)
    {
        if (abit[i])
        {
            sum += bbit[i] * power % mod;
            sum %= mod;
        }
        power = power * 2 % mod;
    }
    cout << sum << endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值