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),然后从低到高枚举 ...
随机推荐
- 在UIWindow上加类似于“回到顶部”的按钮
在公司上个版本的开发中遇到了一个UI布局的小问题: 某个页面需要增加一个分享按钮,但是该页面是二级页面,导航栏右边也已经放置了2个button. 起初和老大谈论这个问题的时候想到的方法是导航栏右边加三 ...
- 【postgresql的使用】
#安装: #初始化: #允许远程登录: #创建数据库并指定用户 #创建用户 #列出数据库 #进入数据库 #查询数据 #or(或)查询 #and ,or(和,或查询) #表连接 #内,外(左右),交叉连 ...
- 关于python文件问题
一.python内部的首行命令 #!/usr/bin/env python #_*_coding:utf8_*_ 第一条命令用于Linux系统中的./命令,用于声明用什么Python解释器.第二条命令 ...
- spring data elasticsearch 使用
很久之前就安装了elasticsearch,一直没用java用过,最近看了一下spring data系列的elasticsearch,这里写一篇心得. 如果尚未安装elasticsearch,可以 参 ...
- BigData:值得了解的十大数据发展趋势
当今,世界无时无刻不在发生着变化.对于技术领域而言,普遍存在的一个巨大变化就是为大数据(Big data)打开了大门,并应用大数据技相关技术来改善各行业的业务并促进经济的发展.目前,大数据的作用已经上 ...
- MIPI调试经验(转载)
目录 一.D-PHY 1.传输模式 2.Lane States 3.Lane Levels 4.操作模式 5.时序要求 二.DSI 1.线路构成 2.两种接口的 LCD 3.数据包类型 4.从控制器到 ...
- IP组播 MulticastChannel接口 DatagramChannel实现
监听者 import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; impo ...
- 关于Win10 环境下Quartus II 15.0器件列表无法下拉的解决方法
不知大家在Windows 10 64位系统环境下使用Quartus II 15.0在新建工程时遇到过这种问题没,在新建工程的过程是选择器件的列表无法下拉,只能看到一个器件型号,如图1所示. 图1 开始 ...
- node从搭建运行项目整体流程
1. 初始化配置基本信息: npm init (自定义配置) npm init -y (一切配置采用默认值) 在当前目录产生package.json文件,有一个dependencies用来记录该项目所 ...
- 课下实践——实现Mypwd
实现Mypwd 学习pwd命令 想要知道当前所处的目录,可以用pwd命令,该命令显示整个路径名. L 目录连接链接时,输出连接路径 P 输出物理路径 研究pwd实现需要的系统调用(man -k; gr ...