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

Input

4 410101101

Output

12

Input

4 5100110101

Output

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.

题意:

给你两个二进制的字符串a和b。(很长)

让你进行一下操作。

把答案加上 a&b的值。然后b右移一位。

上述操作直至b==0

求最后对998244353取模后的的答案。

思路:

我们来看一下样例1。

4 410101101

我们看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=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
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[0]==‘1‘)
    {
        sum[0]=1ll;
    }
    for(ll i=1ll;i<max(n,m);++i)
    {
        if(a[i]==‘1‘)
        {
            sum[i]=sum[i-1]+powmod(2ll,i,mod);
            sum[i]=(sum[i]+mod)%mod;
        }else
        {
            sum[i]=sum[i-1];
        }
    }
    ll ans=0ll;
    for(ll i=0ll;i<max(n,m);++i)
    {
        if(b[i]==‘1‘)
        {
            ans+=sum[i];
            ans=(ans+mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}


原文地址:https://www.cnblogs.com/qieqiemin/p/10802670.html

时间: 2024-11-05 22:32:32

Binary Numbers AND Sum CodeForces - 1066E (前缀和)的相关文章

CodeForces E. Binary Numbers AND Sum

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 

Odd sum CodeForces - 797B

Odd sum CodeForces - 797B 好方法:贪心 糟糕(不用动脑)的方法:dp ans[i][0]表示到第i个和为偶数最大,ans[i][1]表示到第i个和为奇数最大. 但是,仍然容易写挂!(注意细节) 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 typedef long long LL; 6 LL ans[100010][2]

HDU 1390 Binary Numbers

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1390 题目描述: Binary Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2834    Accepted Submission(s): 1758Problem Description Given a positive

[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 problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr

zju 1383 Binary Numbers

#include <iostream> using namespace std; int a[1000]; int f(int n) { int k=0; while(n) { a[k++]=n%2; n/=2; } return k; } int main(int argc, char *argv[]) { int n,m,k,q; while(cin>>q) { while(q--) { cin>>n; m=f(n); for(int i =0,k=0;i<m

杭电 HDU ACM 1390 Binary Numbers

Binary Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3372    Accepted Submission(s): 2026 Problem Description Given a positive integer n, find the positions of all 1's in its binary r

[Leetcode] Binary tree--112. Path Sum

112. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 ret

Binary Numbers(HDU1390)

Binary Numbers 点我 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3820    Accepted Submission(s): 2321 Problem Description Given a positive integer n, find the positions of all 1's in its binary

CF1066EBinary Numbers AND Sum(前缀和,二进制)

题目大意 现在,给你两个位数为 n 和 m 的两个二进制数a,b,现在,我们要进行如下操作: 计算a&b 答案累加上一个操作的值 bbb右移一位,最后一位直接舍弃 现在,请你算出最终的答案,并输出,答案对998244353取模 输入输出格式: 输入格式: 第一行,两个整数n,m,(1≤n,m≤2×105) 第一行,一个长度为n的二进制数a 第一行,一个长度为m的二进制数b 输出格式: 一行,一个数,表示答案 思路: 因为第一个二进制数不动,第二个在动,所以我们可以通过预处理第一个数来获得答案 因