Binary Numbers AND Sum CodeForces - 1066E (前缀和)
You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. 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
4 4
1010
1101
12
4 5
1001
10101
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.
题意:
给你两个二进制的字符串a和b。(很长)
让你进行一下操作。
把答案加上 a&b的值。然后b右移一位。
上述操作直至b==0
求最后对998244353取模后的的答案。
思路:
我们来看一下样例1。
4 4
1010
1101 我们看b,1101 从最后一位看起,最后一位的1,只能&上一次a的最后一位,然后就被消除掉了(右移)
而倒数第二位的0,无论&上多少个数(不管是0还是1) 都不会对答案做出贡献。
继续看倒数第三位的1,他会&上a中的后三位才会被消除掉,会&上a中倒数第2位的1,那么会对答案产生2的贡献。
再看b的第一位的1,他会&上a中的后四位才会被消除掉,&上a中倒数第2个的1和倒数第4位的1,会对答案产生8+2的贡献,
加起来答案就是12。
不知道大家有没有发现什么规律?
b中每一位1都会&上a中对应位置以及后面的所有位。并且b这一位的贡献值就是a中这一位以及之后的数组成的二进制数的十进制数值大小。
那么我们不妨对a进行通过快速幂取模来求出a中每一位数值的前缀和,然后b中每一位1直接去加上那么数值就是贡献。
又因为a和b可能不一样长,我们为了方便把a和b翻转后再计算。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
//string a,b;
int n,m;
char a[maxn];
char b[maxn];
const ll mod=998244353ll;
ll sum[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>m;
cin>>a>>b;
ll x=m-n;
reverse(a,a+n);
reverse(b,b+m); if(a[]=='')
{
sum[]=1ll;
}
for(ll i=1ll;i<max(n,m);++i)
{
if(a[i]=='')
{
sum[i]=sum[i-]+powmod(2ll,i,mod);
sum[i]=(sum[i]+mod)%mod;
}else
{
sum[i]=sum[i-];
}
}
ll ans=0ll;
for(ll i=0ll;i<max(n,m);++i)
{
if(b[i]=='')
{
ans+=sum[i];
ans=(ans+mod)%mod;
}
}
cout<<ans<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Binary Numbers AND Sum CodeForces - 1066E (前缀和)的更多相关文章
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum (二进制,前缀和)
题意:有两个\(01\)字符串\(a\)和\(b\),每次让\(a\)和\(b\)进行与运算,将值贡献给答案,然后将\(b\)右移一位,直到\(b=0\). 题解:因为\(a\)不变,而\(b\)每次 ...
- E. Binary Numbers AND Sum
链接 [http://codeforces.com/contest/1066/problem/E] 题意 给你长度分别为n,m的二进制串,当b>0时,对a,b,&运算,然后b右移一位,把 ...
- CF1066E Binary Numbers AND Sum
思路: 模拟.实现: #include <iostream> using namespace std; ; ], b[]; ]; int main() { int n, m; while ...
- codefores 1066 E. Binary Numbers AND Sum
这个题吧 你画一下就知道了 就拿这个例子来讲 4 5100110101 对于b串的话第5位只会经过a串的第4位,b串的第4位会经过a串的第3位和第4位.....b串的第1和第2位会经过a串的每一位 由 ...
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...
- HDU-1390 Binary Numbers
http://acm.hdu.edu.cn/showproblem.php?pid=1390 Binary Numbers Time Limit: 2000/1000 MS (Java/Others) ...
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
随机推荐
- NOIP2010提高组真题部分整理(没有关押罪犯)
目录 \(NOIP2010\)提高组真题部分整理 \(T1\)机器翻译: 题目背景: 题目描述: 输入输出格式: 输入输出样例: 说明: 题解: 代码: \(T2\)乌龟棋 题目背景: 题目描述: 输 ...
- [HTML辅助方法-Html.Raw()的简单应用]
Html.Raw(); 当我们使用 文本编辑器,存入到数据库中的数据会带 html 标签,如果我们需要在前台显示存入时的相同样式,不输出为带有html标签的字符串 ,不通过富文本显示的话,可以通过ht ...
- 四、日志输出Reporter.log
一.Reporter.log import org.testng.Reporter; public class TestLog { public static void main(String[] a ...
- 深度学习基础——Epoch、Iteration、Batchsize
原文地址:https://www.cnblogs.com/Johnny-z6951/p/11201081.html 梯度下降是一个在机器学习中用于寻找较佳结果(曲线的最小值)的迭代优化算法.梯度的含义 ...
- kotlin之数组
一.使用arrayOf函数定义可以存储任意值的数组 var arr1 = arrayOf(1,2,3,'a') println(arr1[3]) 二.使用arrayOfNulls函数定义数组 var ...
- Fragment 的 replace 和 add 方法的区别?
Fragment 本身并没有 replace 和 add 方法,这里的理解应该为使用 FragmentManager 的 replace 和 add 两种方法切换 Fragment 时有什么不同.我们 ...
- mysql linux环境
创建新用户 create user jnroot@'%' identified by 'Yc@edc#sJn';创建数据库 create database price_monitor DEFAULT ...
- ARM汇编指令特点
根据朱有鹏老师课程笔记整理而来: (汇编)指令是CPU机器指令的助记符,经过编译后会得到一串1 0组成的机器码,由CPU读取执行. (汇编)伪指令本质上不是指令(只是和指令一起写在代码中),它是编译器 ...
- mysql 表字段 记录创建时间和更新时间
sql语句创建: CREATE TABLE `NewTable` ( `id` int NOT NULL AUTO_INCREMENT , `name` varchar(20) NOT NULL , ...
- MySQL-线上数据迁移实战记录
1. 迁移背景和限制条件 随着功能的迭代或者数据表中数据量的增加,将现有数据进行迁移已是工作中经常遇到的事情.通常我们在平时迁移数据数据的时候,只需要用mysqldump.mysqlimport指令就 ...