B. The Bits--Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)
B. The Bits B. The
Bits time limit per test2 seconds memory limit per test256 megabytes
inputstandard input outputstandard 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 a a and b b of length n n. How many
different ways of swapping two digits in a a (only in a a, not b b)
so that bitwise OR of these two numbers will be changed? In other
words, let c c be the bitwise OR of a a and b b, you need to find
the number of ways of swapping two bits in a a so that bitwise OR
will not be equal to c c.Note that binary numbers can contain leading zeros so that length of
each number is exactly n n.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, 010102 OR 100112 = 110112
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 a a so that bitwise OR will be changed.Input
The first line contains one integer n n ( 2 ≤ n ≤ 10^5)
— the number of bits in each number.
The second line contains a binary number a a of length n n.
The third line contains a binary number b b of length n n.
Output
Print the number of ways to swap two bits in a a so that
bitwise OR will be changed.Examples input
5
01011
11001
output
4
input
6
011000
010011
output
6
题意:给一个n,然后输入两个长度为n的01二进制串a,b。对a进行选取两个字符进行交换操作,有多少种操作使生成的新a串 OR(按位或)b 得到的C和原串a OR b得到的C不同
解题思路:这题其实很简单,有效的操作就以下几种情况:
当a[i]='0’且b[i]='0’时 ,a[i]和满足a[j]='0’且b[j]='1’的a[j]交换,这样可以满足得到的c[i]从原先的0变为了1.
当a[i]='1’且b[i]='1’时 ,a[i]和满足a[j]='0’且b[j]='0’的a[j]交换,这样可以满足得到的c[j]从原先的0变为了1.
当a[i]='1’且b[i]='0’时 ,a[i]和满足a[j]='0’且b[j]='1’的a[j]交换,这样可以满足得到的c[j]从原先的0变为了1.
所以只需要求出a[i]='0’且b[i]=‘0’,a[i]='1’且b[i]='1,a[i]='0’且b[i]=‘1’,a[i]='1’且b[i]='0‘,4种情况各自数量就可以
代码如下:
/*
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◆◆◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◆◆◆◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◆◆◆◆◇
◇◇◇◇◆◇◆◆◇◇◇◇◆◆◆◆◆◆◇◇◇◆◆◆◆◆◆◇◇◆◆◆◇◆◆◆◇◇◆◆◆◆◆◇◇◇◇◆◆◆◆◆◇
◇◇◇◆◆◇◆◆◇◇◇◇◇◆◆◇◆◆◇◇◇◆◆◇◇◆◆◇◇◇◆◆◇◆◆◇◇◇◆◆◇◆◆◇◇◇◇◇◆◇◆◆◇
◇◇◇◆◆◆◆◆◆◇◇◇◇◆◇◇◇◆◇◇◇◆◇◇◇◆◆◇◇◇◆◆◇◆◆◇◇◇◆◆◆◆◆◆◇◇◇◇◆◇◇◇◇
◇◇◆◆◇◇◇◆◆◇◇◇◇◆◇◇◇◆◇◇◇◆◇◇◇◆◆◇◇◇◇◆◆◆◇◇◇◇◆◆◇◇◇◇◇◇◇◇◆◇◇◇◇
◇◇◆◆◇◇◇◆◆◇◇◇◇◆◇◇◇◆◇◇◇◆◆◇◇◆◆◇◇◇◇◆◆◆◇◇◇◇◆◆◇◇◆◇◇◇◇◇◆◇◇◇◇
◇◆◆◆◇◇◆◆◆◆◆◇◆◆◆◇◆◆◆◇◇◆◆◆◆◆◆◇◇◇◇◆◆◇◇◇◇◇◆◆◆◆◆◇◇◇◆◆◆◆◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◆◇◇◇◇◇◆◆◆◇◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◆◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
*/
#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long int
#define met(a) memset(a,0,sizeof(a))
#define fup(i,a,n,b) for(int i=a;i<n;i+=b)
#define fow(j,a,n,b) for(int j=a;j>0;j-=b)
using namespace std;
const int maxn = 1e5 + 7;
int main(int argc, char *argv[]) {
int n;
cin >> n;
string a, b;
cin >> a;
cin >> b;
int ans=0;
ll d1=0, d2=0, d3=0, d4=0;//注意开 ll
for (int i = 0; i < n; i++) {
if (a[i] == '0'&&b[i] == '0')d1++;
else if (a[i] == '1'&&b[i] == '0')d2++;
else if (a[i] == '1'&&b[i] == '1')d3++;
else if (a[i] == '0'&&b[i] == '1')d4++;
}
cout << d1*d2+d3*d1+d2*d4 << endl;
return 0;
}