結果

問題 No.295 hel__world
ユーザー Min_25Min_25
提出日時 2017-05-04 21:59:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 566 ms / 5,000 ms
コード長 3,248 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 96,640 KB
実行使用メモリ 10,484 KB
最終ジャッジ日時 2023-10-12 08:10:26
合計ジャッジ時間 5,682 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 1 ms
4,352 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 275 ms
4,352 KB
testcase_29 AC 566 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 69 ms
10,372 KB
testcase_33 AC 70 ms
10,460 KB
testcase_34 AC 70 ms
10,484 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 2 ms
4,352 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 2 ms
4,352 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,352 KB
testcase_43 AC 202 ms
4,352 KB
testcase_44 AC 201 ms
4,352 KB
testcase_45 AC 201 ms
4,352 KB
testcase_46 AC 202 ms
4,348 KB
testcase_47 AC 276 ms
4,348 KB
testcase_48 AC 281 ms
4,352 KB
testcase_49 AC 290 ms
4,348 KB
testcase_50 AC 2 ms
4,352 KB
testcase_51 AC 58 ms
4,356 KB
testcase_52 AC 12 ms
4,348 KB
testcase_53 AC 7 ms
4,612 KB
testcase_54 AC 7 ms
4,620 KB
testcase_55 AC 7 ms
4,652 KB
testcase_56 AC 7 ms
4,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <stack>
#include <queue>

#include <tuple>

#define getchar getchar_unlocked
#define putchar putchar_unlocked

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i64 = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

using i128 = __int128_t;

void solve() {
  const i64 lim = i64(1) << 62;

  i64 counts[26];
  static char str[1000010];

  while (~scanf("%lld", &counts[0])) {
    rep(i, 1, 26) scanf("%lld", &counts[i]);
    scanf("%s", str);
    const int slen = strlen(str);
    rep(i, slen) str[i] -= 'a';

    vector<int> lens[26];
    int freqs[26] = {};
    {
      int i = 0;
      for (int j; i < slen; i = j) {
        int c = str[i]; j = i + 1;
        while (j < slen && str[j] == c) ++j;
        lens[c].push_back(j - i);
        freqs[c] += j - i;
      }
    }

    struct Fraction {
      Fraction() {}
      Fraction(int n, int d) : n(n), d(d) {}
      bool operator < (const Fraction& rhs) const {
        return i64(n) * rhs.d < i64(d) * rhs.n;
      }
      int n, d;
    };

    i128 ans = 1;
    rep(i, 26) {
      if (freqs[i] > counts[i]) {
        ans = 0; break;
      }
    }
    
    if (ans > 0) rep(i, 26) {
      if (freqs[i] == 0) continue;
      i64 rest = counts[i] - freqs[i];

      auto binom = [&] (i64 n, int k) {
        double prod = 1;
        rep(i, k) {
          prod = prod * (n - k + i + 1) / (i + 1);
          if (prod >= lim * 1.01) return i128(lim);
        }
        i128 ret = 1;
        rep(i, k) {
          ret = ret * (n - i) / (i + 1);
        }
        return min(ret, i128(lim));
      };
      sort(lens[i].begin(), lens[i].end());

      bool brute = false;
      i128 prod = 1;
      if (lens[i].size() <= 2) {
        if (lens[i].size() == 1) {
          prod = binom(counts[i], lens[i][0]);
        } else {
          if (lens[i][1] == 1) {
            prod = i128(counts[i] / 2) * (counts[i] - counts[i] / 2);
          } else brute = true;
        }
      } else brute = true;

      if (brute) {
        using P = pair<int, int>;
        priority_queue< pair<Fraction, P> > que;
        rep(id, lens[i].size()) {
          que.push({{lens[i][id] + 1, 1}, {id, 1}});
        }
        for (; rest; --rest) {
          auto d = que.top(); que.pop();
          auto f = d.first;
          int id, j; tie(id, j) = d.second;
          prod = prod * f.n / f.d;
          if (prod >= lim) break;
          auto nf = Fraction(lens[i][id] + j + 1, j + 1);
          que.push({nf, {id, j + 1}});
        }
      }
      if (prod >= lim || (ans *= prod) >= lim) {
        ans = lim; break;
      }
    }
    if (ans >= lim) puts("hel");
    else printf("%lld\n", i64(ans));
  }
}

int main() {
  auto beg = clock();
  solve();
  auto end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
}
0