E. Binary Numbers AND Sum
链接
[http://codeforces.com/contest/1066/problem/E]
题意
给你长度分别为n,m的二进制串,当b>0时,对a,b,&运算,然后b右移一位,把每次a&b的10进制结果累加对 998244353取余
分析
模拟这个过程,但有个技巧就是对b从高位开始求二进制的前缀和
具体看代码
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=998244353;
const ll N=2e5+10;
ll one[N],pw[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll i,j,k,n,m;
string a,b;
//freopen("in.txt","r",stdin);
cin>>n>>m;
cin>>a>>b;
//使位数一致在位数少的前面加0
if(n<m){
a=string(m-n,'0')+a;
}
else if(m<n){
b=string(n-m,'0')+b;
}
n=max(n,m);
one[0]=(b[0]=='1');
for(i=1;i<n;i++) one[i]=one[i-1]+(b[i]=='1');//b高位开始的前缀和
pw[0]=1;
for(i=1;i<N;i++) pw[i]=pw[i-1]*2ll%mod;//计算2的幂次预处理
ll ans=0;
for(i=0;i<n;i++){
ll res=(a[i]=='1');
res=res*pw[n-i-1]%mod;//计算a这个位置的10进制值
ans=(ans+res*one[i]%mod)%mod;//之所以*one[i],是因为b右移的过程,a[i]对应的次数就是b高位开始的前缀和,注意取余mod
}
cout<<ans<<endl;
return 0;
}
E. Binary Numbers AND Sum的更多相关文章
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
- Binary Numbers AND Sum CodeForces - 1066E (前缀和)
You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...
- 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串的每一位 由 ...
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum (二进制,前缀和)
题意:有两个\(01\)字符串\(a\)和\(b\),每次让\(a\)和\(b\)进行与运算,将值贡献给答案,然后将\(b\)右移一位,直到\(b=0\). 题解:因为\(a\)不变,而\(b\)每次 ...
- 【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 ...
随机推荐
- Pygame安装教程
1.python --version 查看安装的Python版本, pip --version 查看安装的pip版本, 升级pip命令: python -m pip install --upgra ...
- java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for ...”
错误信息如下: Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossExce ...
- Visualbox与CentOS 6.4之间鼠标切换
按住键盘右边的Alt键,再按一下(右边)ctrl键,这样可以实现鼠标能在主机与虚拟机之间自由切换.
- 在pycharm中每次运行代码不使用console而使用run
问题:在pycharm中点击run运行程序,发现没有打开run窗口,而是打开的Python console窗口. 解决方法:打开菜单栏run->edit configurations,把下图中的 ...
- OutputStreamWriter与InputStreamReader(转换流)
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import jav ...
- LCA树链剖分
LCA(Lowest Common Ancestor 最近公共祖先)定义如下:在一棵树中两个节点的LCA为这两个节点所有的公共祖先中深度最大的节点. 比如这棵树 结点5和6的LCA是2,12和7的LC ...
- merge-two-sorted-lists (归并排序中的合并)
class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (l1 == NULL) retur ...
- esp8266(2) 智能配置
http://www.arduino.cn/thread-46594-1-1.html http://blog.csdn.net/sadshen/article/details/47049129 ht ...
- day16 Python 内置函数 大体演示想看就看,会用就行
1.abs() 获取绝对值 a = -10 print(a.__abs__()) 结果: 10 2.all() 接收一个迭代器,如果跌电气的所有元素都为真,那么返回True,否则返回False tm ...
- MyBatis+Hibernate+JDBC对比分析
MyBatis目前作为持久层,用的最多,因为它符合互联网开发的变动性,实际开发中需求总会有这样的,那样的变动,MyBatis虽然没有Hibernate那么全自动化,而且对于开发人员的sql能力要求比较 ...