容斥原理——CodeForces 1017B

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AC_jie/article/details/81570537

今天来不昨天的题,这个题想到了思路,但是去重出了问题,一开始拿set去重,直接爆掉,GG。忽然发现自己真正会的东西真少,从今天开始就要脚踏实地的一步一步地将自己的基础打牢。
一会儿整理stl的用法。
先来说一下这个题,本质是一个容斥原理,整天给师弟们讲,自己还是不会用QAQ.

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef long long LL;
int main()
{
    int n;
    while(cin >> n)
    {
        string s1,s2;
        cin >> s1 >> s2;
        LL a = 0,b = 0,c = 0,d = 0;
        for(int i = 0;i < n;++i)
        {
            if(s1[i] == '1') a++;
            else b++;
            if(s2[i] == '0')
                if(s1[i] == '0') c++;
                else d++;
        }
        LL ans = 0;
        ans = (c * a + d * b) - (c * d);//容斥原理
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/AC_jie/article/details/81570537