Codeforces Round #502 Div. 1 + Div. 2 B. The Bits

http://codeforces.com/contest/1017/problem/B

题目大意:给出2个二进制的串(数),可以交换第一个串的2个位置,求有多少种交换可以让交换后的第一个串与第二个串异或结果与之前的不同(相同就不算)

思路:

数据范围为10^5,这个二进制数位数最大10^5位,最大值化为十进制2^10^5,不可能枚举

第一个样例:1->4   3->2/4/5    转化一下对应的情况:(1:01(第一个串该位置为0第二个串该位置为1)   4:10)  (3:00  2:11  4:10  5:11)

第二个样例:2->1/4   3->1/4/5/6  转化对应情况:(2:11  1:00  4:00)   (3:10  1:00  4:00  5:01  6:01)

统计一下:01能与10匹配     00能与11/10匹配    10能与00/01匹配   11能与00匹配

问题转化为求:00的个数*11的个数+00的个数*10的个数+01的个数*10的个数;

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

char s1[200005];
char s2[200005];
unsigned a[5];

int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s%s",s1,s2);
    for (int i = 0; i < n; i ++) {
        if (s1[i] == '0') {
            if (s2[i] == '0') a[1] ++;
            else a[2] ++;
        } 
        else {
            if (s2[i] == '1') a[3] ++;
            else a[4] ++;
        }  
    }
    long long ans = 0;
    ans=a[2]*a[4] + 1ll*a[1]*a[3] + 1ll*a[1]*a[4];
    printf("%lld\n", ans); 
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值