結果

問題 No.295 hel__world
ユーザー Min_25Min_25
提出日時 2017-05-05 01:59:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 3,498 bytes
コンパイル時間 1,245 ms
コンパイル使用メモリ 88,208 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2023-10-12 08:13:26
合計ジャッジ時間 3,054 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,356 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 2 ms
4,356 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 56 ms
9,116 KB
testcase_33 AC 34 ms
9,216 KB
testcase_34 AC 43 ms
9,104 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,352 KB
testcase_39 AC 2 ms
4,352 KB
testcase_40 AC 2 ms
4,352 KB
testcase_41 AC 1 ms
4,352 KB
testcase_42 AC 2 ms
4,352 KB
testcase_43 AC 2 ms
4,352 KB
testcase_44 AC 2 ms
4,352 KB
testcase_45 AC 1 ms
4,352 KB
testcase_46 AC 2 ms
4,352 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,352 KB
testcase_52 AC 2 ms
4,352 KB
testcase_53 AC 11 ms
4,484 KB
testcase_54 AC 11 ms
4,504 KB
testcase_55 AC 9 ms
4,552 KB
testcase_56 AC 9 ms
4,516 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 inf = i64(1) << 62;
  const i128 denom = 2e20;

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

    vector<int> lens[26];
    int freqs[26] = {};
    lens[int(str[0])].push_back(1); freqs[int(str[0])]++;
    rep(i, 1, slen) {
      if (str[i] == str[i - 1]) lens[int(str[i])].back()++;
      else lens[int(str[i])].push_back(1);
      freqs[int(str[i])]++;
    }

    i128 ans = 1;
    rep(i, 26) {
      if (counts[i] < freqs[i]) {
        ans = 0; break;
      }
      if (freqs[i] == 0 || counts[i] == freqs[i] || ans >= inf ) {
        continue;
      }

      auto binom = [&] (i64 a, int k) {
        i128 ret = 1;
        rep(i, k) {
          ret = ret * (a - k + 1 + i) / (i + 1);
          if (ret >= inf) return inf;
        }
        return i64(ret);
      };

      i128 prod = 1;
      if (lens[i].size() == 1) {
        prod = binom(counts[i], lens[i][0]);
      } else {
        i64 rest = counts[i] - freqs[i];
        if (rest >= 2e9) {
          prod = inf;
        } else {
          i128 ok = denom + 1, ng = slen * denom + 1;
          bool exact = false;
          while (ng - ok > 1) {
            i128 mid = (ok + ng) >> 1;
            i128 total = 0;
            for (auto l : lens[i]) total += (l * denom) / (mid - denom);
            if (total >= rest) {
              ok = mid;
              if (total == rest) {
                exact = true;
                ng = mid;
              }
            } else if (total < rest) {
              ng = mid;
            }
          }
          for (auto l : lens[i]) {
            i64 k = (l * denom) / (ng - denom);
            if (k) {
              i64 b = binom(l + k, l);
              prod = min(i128(inf), prod * b);
              rest -= k;
            }
          }
          assert(rest >= 0);
          if (!exact && prod < inf) {
            for (auto l : lens[i]) {
              i64 k1 = (l * denom) / (ok - denom);
              i64 k2 = (l * denom) / (ng - denom);
              if (k1 > k2) {
                assert(k1 == k2 + 1);
                prod = prod * (k1 + l) / k1;
                prod = min(i128(inf), prod);
                if (prod >= inf) break;
                if (--rest == 0) break;
              }
            }
            if (prod < inf) assert(rest == 0);
          }
        }
      }
      if (prod >= inf || (ans *= prod) >= inf) {
        ans = inf;
      }
    }
    if (ans >= inf) 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