codeforces 17C Balance

Posted wuyuanyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 17C Balance相关的知识,希望对你有一定的参考价值。

codeforces 17C Balance

题意

题解

代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "
"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---

const int N = 155, P = 51123987;

int n, m;
int ne[N][3], f[N][55][55][55];
string s;

void add(int &a, int b) {
    if((a+=b)>=P) a-=P;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cin >> n >> s;
    s = " " + s;
    memset(ne, -1, sizeof(ne));
    for(int i = sz(s)-1; ~i; --i) {
        rep(j, 0, 3) ne[i][j] = ne[i+1][j];
        if(i) ne[i][s[i]-‘a‘] = i;
    }
    m = (n+2)/3;
    f[0][0][0][0] = 1;
    rep(i, 0, n+1) {
        rep(a, 0, m+1) rep(b, 0, m+1) rep(c, 0, m+1) {
            rep(j, 0, 3) if(~ne[i][j]) {
                int t = ne[i][j];
                add(f[t][a+(j==0)][b+(j==1)][c+(j==2)], f[i][a][b][c]);
            }
        }
    }
    int ans = 0, l = n/3, r = m;
    rep(i, 1, n+1) {
        rep(a, l, r+1) rep(b, l, r+1) rep(c, l, r+1) if(a+b+c==n) {
            add(ans, f[i][a][b][c]);
        }
    }
    cout << ans << endl;
    return 0;
}

以上是关于codeforces 17C Balance的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 1606A AB Balance

[Codeforces 1242C]Sum Balance

CodeForces 1299C Water Balance

CodeForces - 1504C Balance the Bits(思维+构造)

Codeforces 1300E. Water Balance

codeforces 1242C/1243 E . Sum Balance