CF 502 B-The Bits(找规律)

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≤10^5 2≤n≤10^5) — 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

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

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

题目大意:给出2个二进制的串(数),可以交换第一个串的2个位置,求有多少种交换可以让交换后的第一个串与第二个串异或结果与之前的不同(相同就不算)

思路:

数据范围为10^5,也就是说这个二进制数位数最大10^5位,最大值化为十进制2^10^5,是不可能枚举的,所以,这道题一定是有问题的,以CF的尿性,肯定是有公式/规律的。脑洞一下它的样例解释。

第一个样例:1->4   3->2/4/5    转化一下对应的情况:(1:01(第一个串该位置为0第二个串该位置为1)   4:10)  (3:00  2:11  4:10  5:11)

第二个样例:2->1/4   3->1/4/5/6  转化对应情况:(2:11  1:00  4:00)   (3:10  1:00  4:00  5:01  6:01)

统计一下:01能与10匹配     00能与11/10匹配    10能与00/01匹配   11能与00匹配

所以问题转化为求:00的个数*11的个数+00的个数*10的个数+01的个数*10的个数;(这规律也是蛋疼)

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s1[200005];
char s2[200005];
unsigned a[5];
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s%s",s1,s2);
    for(int i=0;i<n;i++)
    {
        if(s1[i]=='0')
        {
		    if(s2[i]=='0') a[1]++;
            else a[2]++;
        } 
        else
        {
            if(s2[i]=='1') a[3]++;
            else a[4]++;
        }  
    }
    long long ans=0;
    ans=a[2]*a[4]+1ll*a[1]*a[3]+1ll*a[1]*a[4];
    printf("%lld\n",ans); 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/81531230