1. 程式人生 > >cf1066E Binary Numbers AND Sum

cf1066E Binary Numbers AND Sum

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