目录
题目描述
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,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 aa so that bitwise OR will be changed.
Input
The first line contains one integer nn (2≤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), (2,3), (3,4), and (3,5).
In the second example, you can swap bits that have indexes (1,2), (1,3), (2,4), (3,4), (3,5), and (3,6).
题意分析
题意:给你两个二进制数,A、B,交换A的两个字符 求能使 A与B的或 发生改变的方法数。
把其分成4种情况。判断一下,何种情况下值会发生改变。然后愉快的将其相乘累加即可
虽然我觉得INT不会超,但还是WA了,记得要使用 long long!
AC代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 100005
using namespace std;
char s1[MAXN];
char s2[MAXN];
int x[MAXN];
int main()
{
long long n,i,sum1=0,sum2=0,sum3=0,sum4=0;
cin>>n;
cin>>s1;
cin>>s2;
long long sum=0;
for(i=0;i<n;i++)
{
if(s1[i]=='0')
{
if(s2[i]=='1')
{
sum1++;
}
else
{
sum4++;
}
}
else
{
if(s2[i]=='0')
{
sum2++;
}
else
{
sum3++;
}
}
}
sum = sum1*sum2+sum2*sum4+sum3*sum4;
cout<<sum<<endl;
return 0;
}