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, 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.
Examples
input
Copy
5 01011 11001
output
Copy
4
input
Copy
6 011000 010011
output
Copy
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).
大体含义:有两个长度都为n的二进制数列,要求改变第一个序列任何两个数的位置,使其两者的数相与不同于原来的相与的结果
思路:一看n最大为1e5,爆搜肯定是不过的,因此这道题一定是有问题的,以CF的尿性,肯定是有公式/规律的。脑洞一下它的样例解释。(这句话很精辟啊)
看第一组样例,自己模拟一遍就会发现这样一个规律:
10
01这样一对交换不管位置在哪都会改变原先的值
10
10这样一对也可以
10
00这样也行
因此就会得到这样的规律,这样就转化成求解:00的个数*11的个数+00的个数*10的个数+01的个数*10的个数;(坑)
#include <bits/stdc++.h>
using namespace std;
string a,b;
long long g[2][2],n;
int main(){
cin>>n>>a>>b;
while(n--)
g[a[n]-'0'][b[n]-'0']++;
cout<<g[0][0]*(g[1][0]+g[1][1])+g[0][1]*g[1][0]<<endl;
}