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 "\n"
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;
}

原文地址:https://www.cnblogs.com/wuyuanyuan/p/9255381.html

时间: 2024-07-31 05:35:22

codeforces 17C Balance的相关文章

Codeforces

Codeforces 7E 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <map> 6 using namespace std; 7 map<string,int> Map; 8 int KASE; 9 string Str,t; 10 char Tmp[110]; 11 inline

codeforces 1242C/1243 E . Sum Balance

弄了一整晚 各种bug ...最后超时,差点放弃... 然后...没指望地 随意剪了个枝.,然后......居然做出来了... E. Sum Balance 题目大意:有k个箱子(1<=k<=15),在第i个箱子里有ni(1<=n<=5000)个整数 ( |aij|<=1e9).所有整数都是不同的.对 每个箱子 都  进行这样的操作:从每个箱子中取出一个整数,并放入 另一个或原本的 箱子中.问能否进行这样子的操作使得 操作结束后,每个箱子的数字的和相等. 赛后补题,看了cod

[Codeforces 1242C]Sum Balance

Description 题库链接 给你 \(k\) 个盒子,第 \(i\) 个盒子中有 \(n_i\) 个数,第 \(j\) 个数为 \(x_{i,j}\).现在让你进行 \(k\) 次操作,第 \(i\) 次操作要求从第 \(i\) 个盒子中取出一个元素(这个元素最开始就在该盒子中),放入任意一个你指定的盒子中,要求经过 \(k\) 次操作后 所有盒子元素个数和最开始相同: 所有盒子元素总和相等 询问是否存在一种操作方式使之满足,若存在,输出任意一种方案即可. \(1\leq k\leq 15

Codeforces Round #599 E - Sum Balance

tarjan缩点,枚举子集. 首先avg即平均值是可以直接求出来的,我们假设第i个盒子给出去a,那么平衡需要的是,avg-sumi+a,我们把所有数字开一个map保存下来,如果有该数字,连一条有向边,从a到avg-sumi+a. 可以看到,因为题目说了unique ,所以一个点的出度是固定的,即1.现在要求所有盒子平衡,且每个盒子必须拿出来一个值.那么我们从a连出了一条有向边,如果这是一个可行解的话,那么a会处在一个环中,即a肯定也会被需要,而且还处在当前这条链中.因出度为1,环是简单环,是一条

CodeForces 1299C Water Balance

首先,$\mathcal O(n^2)$ 的思路十分好想. 大概就是说,我们发现答案序列是单调不降的,所以倒着来,对于每个位置 $i$,我们找一个它后面的位置 $j$,使得 $\frac{\sum_{k=i}^{j} a_k}{j-i+1}$ 是最小的,然后把这一段全部赋值为这个值.这样,我们就在优先确保前面的值更优时,得到了一个可靠的贪心策略. 因为 $10^6$ 肯定要更好的方法,所以考虑优化.当处理位置 $i$ 的时候,$(i, n]$ 已经处理好了,成为了一个不降的,若干个连续段组成的数

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造

A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (f

Codeforces Round #308 (Div. 2)

A. Vanya and Table Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rect

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C - Table Tennis Game 2

地址:http://codeforces.com/contest/765/problem/C 题目: C. Table Tennis Game 2 time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Misha and Vanya have played several table tennis sets. Each set co

Codeforces Round #186 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/313 A. Ilya and Bank Account time limit per test:2 seconds memory limit per test:256 megabytes Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obl