Codeforces Round #515 (Div. 3)E. Binary Numbers AND Sum【數學】
E. Binary Numbers AND Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
Note
The algorithm for the first example:
- add to the answer 10102 & 11012=10002=81010102 & 11012=10002=810 and set b:=110b:=110;
- add to the answer 10102 & 1102=102=21010102 & 1102=102=210 and set b:=11b:=11;
- add to the answer 10102 & 112=102=21010102 & 112=102=210 and set b:=1b:=1;
- add to the answer 10102 & 12=02=01010102 & 12=02=010 and set b:=0b:=0.
So the answer is 8+2+2+0=128+2+2+0=12.
The algorithm for the second example:
- add to the answer 10012 & 101012=12=11010012 & 101012=12=110 and set b:=1010b:=1010;
- add to the answer 10012 & 10102=10002=81010012 & 10102=10002=810 and set b:=101b:=101;
- add to the answer 10012 & 1012=12=11010012 & 1012=12=110 and set b:=10b:=10;
- add to the answer 10012 & 102=02=01010012 & 102=02=010 and set b:=1b:=1;
- add to the answer 10012 & 12=12=11010012 & 12=12=110 and set b:=0b:=0.
So the answer is 1+8+1+0+1=111+8+1+0+1=11.
一道數學題,按照題目給的操作進行,這道題我是看大佬的程式碼,數學證明還沒想明白。。。。。一個星期內補上。
#include <bits/stdc++.h>
#define mod 998244353
using namespace std;
const int MAXN = 2000010;
char a[MAXN],b[MAXN];
int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
scanf("%s%s",a,b);
long long x = 0,y = 1;
for(int i = 0; i < m; i++){
if(b[i] == '1') x++;
}
long long ans = 0;
for(int i = 1; i <= n; i++){
if(a[n - i] == '1'){
ans = (ans + y * x) % mod;
}
y = y * 2 % mod;
if(b[m - i] == '1') x--;
if(!x) break;
}
printf("%lld\n",ans);
}
return 0;
}