# Cf1066ebinary Numbers and Sum (prefix and, binary)

Source: Internet
Author: User

Main topic

Now, give you two digits forn and m two binary numbers a,b Now, let's do the following:

• Calculate a&b
• The answer accumulates the value of the previous action
• BBB, move right one, the last one to abandon directly

Input and output format: input format:

First row, two integers n , m , (1≤n,m≤2x105)

First line, a binary number a with a length of n

First line, a binary number b with a length of M

Output format:

A line, a number, that represents the answer

Ideas:

Because the first binary number is not moving, the second one is moving, so we can get the answer by preprocessing the first number.

Because it is with, so only two of them are only 1 o'clock will have the answer to the contribution

So, let's say this example:

1001
11010

He's going to have some of the following.

01001
11010

1001
1101

1001
0110

1001
0011

1001
0001

We will find that the first 1 differences and the first 1 above and the last 1 XOR each other and contribute to the answer.

So the 1 contribution to the answer is 8+1=9 .

Each of the 1 in Y is done as above

You can come up with answers

However, we will find that the complexity of the answer is O (nxm) .

So we're going to preprocess

Use the prefix and run x again

Complexity optimization to O (m+n)

Code:

`#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineRii Register int i#defineRij Register Int J#defineP 998244353#defineint long Longusing namespacestd;intb;intx,y,bs,qzh;voidYcl () {bs=1;  for(rii=1; i<=200002; i++) {Bs[i]=bs[i-1]*2; Bs[i]%=p; }}signed Main () {scanf ("%lld%lld\n",&a,&b);  for(rii=1; i<=a;i++) {X[i]=getchar ()-'0';    } getchar ();  for(rii=1; i<=b;i++) {Y[i]=getchar ()-'0';    } YCL ();  for(rii=a;i>=1; i--) {Qzh[a-i+1]=qzh[a-i]; Qzh[a-i+1]+=x[i]*bs[a-i]; Qzh[a-i+1]%=p; }    if(b>a) { for(rii=a+1; i<=b;i++) {Qzh[i]=qzh[i-1]; }    }//For (rii=1;i<=b;i++)//  {//printf ("%d", Qzh[i]);//  }    intans=0;  for(rii=1; i<=b;i++) {ans+=y[i]*qzh[b-i+1]; Ans%=p; } cout<<ans%p;}`

Cf1066ebinary Numbers and Sum (prefix and, binary)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.