Codeforces Round #515 (Div. 3)E. Binary Numbers AND Sum【数学】

本文探讨了一个复杂的算法问题,涉及两个巨大的二进制整数的运算。通过重复执行特定操作,包括按位与运算和除以2取整,文章详细解释了如何计算最终结果,并给出了具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

  1. add to the answer 10102 & 11012=10002=81010102 & 11012=10002=810 and set b:=110b:=110;
  2. add to the answer 10102 & 1102=102=21010102 & 1102=102=210 and set b:=11b:=11;
  3. add to the answer 10102 & 112=102=21010102 & 112=102=210 and set b:=1b:=1;
  4. 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:

  1. add to the answer 10012 & 101012=12=11010012 & 101012=12=110 and set b:=1010b:=1010;
  2. add to the answer 10012 & 10102=10002=81010012 & 10102=10002=810 and set b:=101b:=101;
  3. add to the answer 10012 & 1012=12=11010012 & 1012=12=110 and set b:=10b:=10;
  4. add to the answer 10012 & 102=02=01010012 & 102=02=010 and set b:=1b:=1;
  5. 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;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值