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
比如01010 OR 10011 = 11011。问如果交换第一个串中的某两个位能够使按位或的运算结果改变,这样的交换有多少种?
解题思路:我们会发现按位或的运算方式如果两个数中只要有1按位或的结果就是1!这样就会发现规律。
① 如果s1串的位置为0并且y串的位置也为0的话,那么只要用1和s1串这个位置的0交换,这个位置的按位或值就一定会发生变换。
② 如果s1串的位置为1,并且s2串的位置为0的话,那么如果另一个位置s1串为0,s2串为1,交换s1串的这两个位置,按位或值也会发生变换。
除了上述的情况外,交换s1串都不会使得按位或值发生变换。所以我们只要统计相应的情况的个数然后对应相乘再相加就可以了。
#include<cstdio>
#include<cstring>
#include<algorithm>
const int MAX=1e5+;
#define ll long long int
using namespace std;
char s1[MAX],s2[MAX];
int main()
{
int n,i;
ll ans,a,b,c,d;
scanf("%d",&n);
getchar();
scanf("%s%s",s1,s2);
a=b=c=d=;
ans=;
for(i=; i<n; i++)
{
if(s1[i]==''&&s2[i]=='')
{
a++;
}
else if(s1[i]==''&&s2[i]=='')
{
b++;
}
else if(s1[i]==''&&s2[i]=='')
{
c++;
}
else if(s1[i]==''&&s2[i]=='')
{
d++;
}
}
ans=b*(a+c)+c*d;
printf("%lld\n",ans);
return ;
}
The Bits (思维+找规律)的更多相关文章
- Codeforces D. Little Elephant and Interval(思维找规律数位dp)
题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...
- [CodeForces - 848B] Rooter's Song 思维 找规律
大致题意: 有一个W*H的长方形,有n个人,分别站在X轴或Y轴,并沿直线向对面走,第i个人在ti的时刻出发,如果第i个人与第j个人相撞了 那么则交换两个人的运动方向,直到走到长方形边界停止,问最后每个 ...
- codeforces Round #440 B Maximum of Maximums of Minimums【思维/找规律】
B. Maximum of Maximums of Minimums time limit per test 1 second memory limit per test 256 megabytes ...
- 1536 不一样的猜数游戏 dp思维 + 找规律
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1536 首先,要知道值为n的答案,则可以这么去想,知道值为n - 1的答案 ...
- 牛客国庆集训day6 B Board (模拟标记思维或找规律或分块???)
链接:https://www.nowcoder.com/acm/contest/206/B来源:牛客网 题目描述 恬恬有一个nx n的数组.她在用这个数组玩游戏: 开始时,数组中每一个元素都是0. 恬 ...
- 【春训团队赛第四场】补题 | MST上倍增 | LCA | DAG上最长路 | 思维 | 素数筛 | 找规律 | 计几 | 背包 | 并查集
春训团队赛第四场 ID A B C D E F G H I J K L M AC O O O O O O O O O 补题 ? ? O O 传送门 题目链接(CF Gym102021) 题解链接(pd ...
- Just Random HDU - 4790 思维题(打表找规律)分段求解
Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...
- HDU1005Number Sequence(找规律)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 51nod_1831: 小C的游戏(Bash博弈 找规律)
题目链接 此类博弈不需要考虑sg函数,只需要确定必胜态和必败态,解题思路一般为打败先打表找规律,而后找规律给出统一的公式.打表方式:给定初始条件(此题中为ok[0]=ok[1]=0),然后从低到高枚举 ...
随机推荐
- Java接口和抽象类详解
父类定义了相关子类的共有属性和行为.而接口可以定义类的共同行为(包括非相关的类). 了解接口前,先来说说抽象类.抽象类介乎于普通类和接口之间,提供部分实现方法以及未实现方法,可以看作为一个半成品. 抽 ...
- canvas制作的烟花效果
最近感觉canvas挺有意思的,在业余时间没事研究了一下,参考过网上一些思路,话不多说,开始啦. github地址:https://github.com/aWhiteBear/fireworks 演示 ...
- 建立复数类Complex,并且进行赋值,求和,取模等操作
#include "pch.h" #include <iostream> #include<cmath> using namespace std; clas ...
- python django web 端文件上传
利用Django实现文件上传并且保存到指定路径下,其实并不困难,完全不需要用到django的forms,也不需要django的models,就可以实现,下面开始实现. 第一步:在模板文件中,创建一个f ...
- MongoDB DBA 实践5-----复制集集群的数据同步和故障转移
(1)复制集集群的数据同步 1>主节点数据库test,在其中goods集合中加入一个文档. 2>在副节点中查看 注意:SECONDARY是不允许读写的,要使用rs.slaveOk()获得读 ...
- php设置文件类型content-type
在PHP中可以通过header函数来发送头信息,还可以设置文件的content-type,下面整理了一些常见文件类型对于的content-type值. //date 2015-06-22//定义编码h ...
- Keil MDK最新版 5.25介绍及下载地址
看到Keil MDK又出新版咯,分享给大家 Keil MDK-ARM 5.25 uVision5开发工具下载地址:http://www.myir-tech.com/soft.asp?id=1140 K ...
- UVA 400 - Unix ls (Unixls命令)
csdn : https://blog.csdn.net/su_cicada/article/details/86773007 例题5-8 Unixls命令(Unix ls,UVa400) 输入正整数 ...
- 20155232 实验一《Java开发环境的熟悉》实验报告
20155232 实验一<Java开发环境的熟悉>实验报告 实验内容 使用JDK编译.运行简单的Java程序: 使用Eclipse 编辑.编译.运行.调试Java程序 实验要求 没有Lin ...
- VirtualBox上LInux命令初步学习
大二的寒假已经接近了尾声,寒假期间我初步使用了VirtualBox虚拟机软件,并安装了ubuntu的操作系统进行了Linux语言的学习.然而寒假期间的学习没有太多的计划,纯粹是为了完成作业而应付性的学 ...