题目: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;
}