The Bits CodeForces - 1017B

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.

扫描二维码关注公众号,回复: 2765427 查看本文章

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) .

分析:本题为一道交换二进制码顺序使其二者异或改变的题,需注意以下两点。

1.数据为10的5次方如果时间复杂度为O(n2)的话就超时了。

2.数据输入问题。直接输数字是肯定行不通的了。

思路1.

/*aORb变的情况    
1.a 0对应b 0.a 0用1交换。    
2.a 1对应b 0.a 1用0交换。    
3.a 0,b 1,与a 1,b0的交换    
4.a 1,b 1,与a 0,b 0的交换。此法超时*/
一开始的思路,遍历两个数组按上述关系做。很显然会超时。

2.
 

/*2.考虑复杂度为n的方法:    
1.b为1的位置无论怎么交换异或结果均为1.所以仅需考虑b为0的情况。    
2.b为0看a的情况    
如果a为0找a为1的交换反之找a为0的交换。
记录位置避免重复交换*/

上AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn = 100005;
int a[maxn], b[maxn];
char ch1[maxn], ch2[maxn];
int sum[5];
/*
aORb变的情况
	1.a 0对应b 0.a 0用1交换。
	2.a 1对应b 0.a 1用0交换。
	3.a 0,b 1,与a 1,b0的交换
	4.a 1,b 1,与a 0,b 0的交换。
此法超时*/
/*2.
考虑复杂度为n的方法:
	1.b为1的位置无论怎么交换异或结果均为1.所以仅需考虑b为0的情况。
	2.b为0看a的情况
	如果a为0找a为1的交换反之找a为0的交换。记录位置避免重复交换

*/
int main()
{
	int n;
	cin >> n;
	scanf("%s", ch1); scanf("%s", ch2);
	for (int i = 0;i < n; i++)
	{
		a[i] = ch1[i] - '0';
		sum[a[i]]++;
	}
	for (int i = 0; i < n; i++)
	{
		b[i] = ch2[i] - '0';
	}
	long long ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (b[i] == 0)
		{
			if (a[i] == 1)
			{
				ans += sum[0];
				sum[1]--;
			}
			else
			{
				ans += sum[1];
				sum[0]--;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/fighting_yifeng/article/details/81606948