B. The Bits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question:
Given two binary numbers aa and bb of length nn. How many different ways of swapping two digits in aa (only in aa, not bb) so that bitwise OR of these two numbers will be changed? In other words, let cc be the bitwise OR of aa and bb, you need to find the number of ways of swapping two bits in aa so that bitwise OR will not be equal to cc.
Note that binary numbers can contain leading zeros so that length of each number is exactly nn.
Bitwise OR is a binary operation. A result is a binary number which contains a one in each digit if there is a one in at least one of the two numbers. For example, 010102010102 OR 100112100112 = 110112110112.
Well, to your surprise, you are not Rudolf, and you don't need to help him…… You are the security staff! Please find the number of ways of swapping two bits in aa so that bitwise OR will be changed.
Input
The first line contains one integer nn (2≤n≤1052≤n≤105) — the number of bits in each number.
The second line contains a binary number aa of length nn.
The third line contains a binary number bb of length nn.
Output
Print the number of ways to swap two bits in aa so that bitwise OR will be changed.
Examples
input
Copy
5 01011 11001
output
Copy
4
input
Copy
6 011000 010011
output
Copy
6
Note
In the first sample, you can swap bits that have indexes (1,4)(1,4), (2,3)(2,3), (3,4)(3,4), and (3,5)(3,5).
In the second example, you can swap bits that have indexes (1,2)(1,2), (1,3)(1,3), (2,4)(2,4), (3,4)(3,4), (3,5)(3,5), and (3,6)(3,6).
题意:给出两个01串,x,y。问,只交换1次x串中任意两个元素的位置,使得x亦或y发生改变,这样的交换有多少种。
思路:现在我们来考虑交换之后亦或发生变换的情况有哪些。
① 如果x串的位置为0并且y串的位置也为0的话,那么只要用1和x串这个位置的0交换,这个位置的亦或值就一定会发生变换。
② 如果x串的位置为1,并且y串的位置为0的话,那么如果另一个位置x串为0,y串为1,交换x串的这两个位置,亦或值也会发生变换。
除了上述的情况外,交换x串都不会使得亦或值发生变换。所以我们只要统计相应的情况的个数然后对应相乘再相加就可以了。
(注意用long long)
#include
using namespace std;
const int Max=1e5+10;
char x[Max],y[Max];
int main()
{
ios::sync_with_stdio(false);
int n;
long long a,b,c,d;
cin>>n;
a=b=c=d=0;
for(int i=0;i>x[i];
if(x[i]=='1') a++;
}
for(int i=0;i>y[i];
for(int i=0;i