• 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
    ",ans);
    38     return 0;
    39 }
  • 相关阅读:
    金蝶k3wise 核算项目、辅助资料
    金蝶——“免、抵、退”税操作说明及帐务处理
    阿里云各Linux发行版netcore兼容性评估报告---来自大石头的测试
    金蝶KIS&K3助记码SQL数据库批量刷新
    华为交换机批量加入 Vlan 方法
    华为设备默认console密码
    SQL查询数据并插入新表
    ORACLE删除当前用户下所有的表的方法
    【转】使用Navicat for Oracle新建表空间、用户及权限赋予
    [转]spring mvc注解方式实现向导式跳转页面
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9546511.html
Copyright © 2020-2023  润新知