Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) B. The Bits

题目传送门:http://codeforces.com/contest/1017/problem/B

本题可以找规律,因为是or运算,所以第二个01串上的1不用管,因为无论怎么调换第一个01串,1所对应的那位结果永远是1,所以只用看第二个01串的0位。比如样例,第一个是01011,第二个是11001,只用看第3,4位即可。第3位的0对应的是0,所以在第一个字符串找1,而1有两种,一种是0对应的1,还有一种是1对应的1。为甚么要分两种呢?因为0对应的1可能会被重复计算,所以我从后面计算后缀,也就是出现的0对应的0,0对应的1。还有一种是1对应的0和1对应的1,折中不用担心被重复计算,每次计算都需要加上。
在解释一下,上文中0对应的1是指str2的0位对应的str1的1位。
注意,最后的结果要用long long ,int 会WA,已经犯了好几次这样的错误了。呜呜呜
废话少说,具体看AC代码:

这里写代码片
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>

using namespace std;
typedef unsigned long long ULL;
const int maxn = 1e5 + 100;
string str1,str2;
int sum1[maxn],sum2[maxn];
int main(){
    int n = 0;
    cin >> n;
    cin >> str1 ;
    cin >> str2;
    memset(sum1,0,sizeof(sum1));
    memset(sum2,0,sizeof(sum2));
    int cnt1 = 0,cnt2 = 0,cnt3 = 0,cnt4 = 0;

    for(int i = n - 1;i >= 0;--i){
        if(str1[i] == '0' && str2[i] == '0' ){
            sum1[i] = cnt1;
        }
        if(str1[i] == '1' && str2[i] == '0' ){
            sum2[i] = cnt3;
        }
        if(str2[i] == '0' && str1[i] == '1'){
            ++cnt1;
        }
        if(str2[i] == '1' && str1[i] == '1'){
            ++cnt2;
        }
        if(str2[i] == '0' && str1[i] == '0'){
            ++cnt3;
        }
        if(str2[i] == '1' && str1[i] == '0'){
            ++cnt4;
        }
    }

    ULL ans = 0;
    for(int i = 0;i < n;++i){
        if(str1[i] == '0' && str2[i] == '0'){
            ans += sum1[i] + cnt2;
        }
        if(str1[i] == '1' && str2[i] == '0'){
            ans += sum2[i] + cnt4;
        }
    }

    cout << ans << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值