The Bits (思维+找规律)

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, 01010 OR 10011 = 11011.

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
 
题目意思:给你两个长度为n,全都是由01组成的串,这两个串按照按位或的运算方式计算会得到一个结果。
比如01010 OR 10011 = 11011。问如果交换第一个串中的某两个位能够使按位或的运算结果改变,这样的交换有多少种?

解题思路:我们会发现按位或的运算方式如果两个数中只要有1按位或的结果就是1!这样就会发现规律。
①  如果s1串的位置为0并且y串的位置也为0的话,那么只要用1和s1串这个位置的0交换,这个位置的按位或值就一定会发生变换。
② 如果s1串的位置为1,并且s2串的位置为0的话,那么如果另一个位置s1串为0,s2串为1,交换s1串的这两个位置,按位或值也会发生变换。
除了上述的情况外,交换s1串都不会使得按位或值发生变换。所以我们只要统计相应的情况的个数然后对应相乘再相加就可以了。

 
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 const int MAX=1e5+10;
 5 #define ll long long int
 6 using namespace std;
 7 char s1[MAX],s2[MAX];
 8 int main()
 9 {
10     int n,i;
11     ll ans,a,b,c,d;
12     scanf("%d",&n);
13     getchar();
14     scanf("%s%s",s1,s2);
15     a=b=c=d=0;
16     ans=0;
17     for(i=0; i<n; i++)
18     {
19         if(s1[i]=='1'&&s2[i]=='1')
20         {
21             a++;
22         }
23         else if(s1[i]=='0'&&s2[i]=='0')
24         {
25             b++;
26         }
27         else if(s1[i]=='1'&&s2[i]=='0')
28         {
29             c++;
30         }
31         else if(s1[i]=='0'&&s2[i]=='1')
32         {
33             d++;
34         }
35     }
36     ans=b*(a+c)+c*d;
37     printf("%lld\n",ans);
38     return 0;
39 }

 

转载于:https://www.cnblogs.com/wkfvawl/p/9546511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值