Description
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.
Sample Input
Input
5 01011 11001
Output
4
Input
6 011000 010011
Output
6
只要找规律就能a的一道题,可惜推错了公式,试了几组样列都没毛病,还是卡在后边的样例。正确的应该是把四种情况都列出来,我在做题的时候只把他们分成上下相同和不同两种情况。。。。
ac代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
using namespace std;
typedef long long int LL;
const int MAXN=1e5+10;
const int INF = 0x3f3f3f3f;
char a[MAXN],b[MAXN];
int main()
{
LL n;
LL s1,s2,s3,s4;
cin>>n;
scanf("%s",a);
scanf("%s",b);
s1=s2=s3=s4=0;
for(int i=0; i<n; i++)
{
if(a[i]=='1'&&b[i]=='1')
s1++;
else if(a[i]=='0'&&b[i]=='0')
s2++;
else if(a[i]=='1'&&b[i]=='0')
s3++;
else if(a[i]=='0'&&b[i]=='1')
s4++;
}
printf("%lld\n",s2*(s1+s3)+s3*s4);
return 0;
}