B. The Bits
http://codeforces.com/problemset/problem/1017/B
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
5 01011 11001
output
4
input
6 011000 010011
output
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).
题意:两个长度为n的01字符串x,y。交换x的某个字符,y不变,使x|y(异或)改变。有几种方法?
思路:当yi为"1"时,不管xi怎么交换,第i个位置的异或值永远为1。所以,只要把yi=0的位置对应的xi改变即可。但注意会重复。
代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#define inf 0x3f3f3f3f
using namespace std;
char x[100005];
char y[100005];
int main()
{
int n;
long long a,b,c,d;
cin>>n;
a=b=c=d=0;
for(int i=0;i<n;i++)
{
cin>>x[i];
if(x[i]=='1')
a++;
}
for(int i=0;i<n;i++)
cin>>y[i];
for(int i=0;i<n;i++)
{
if(x[i]=='0'&&y[i]=='0')
b++;
if(x[i]=='0'&&y[i]=='1')
c++;
if(x[i]=='1'&&y[i]=='0')
d++;
}
long long sum=a*b+c*d;
cout<<sum<<endl;
return 0;
}