結果

問題 No.295 hel__world
ユーザー te-shte-sh
提出日時 2018-02-01 15:34:35
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 2,281 ms / 5,000 ms
コード長 1,794 bytes
コンパイル時間 11,166 ms
コンパイル使用メモリ 671,832 KB
実行使用メモリ 20,880 KB
最終ジャッジ日時 2023-09-03 18:34:42
合計ジャッジ時間 15,626 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,356 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,356 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 1 ms
4,360 KB
testcase_12 AC 2 ms
4,356 KB
testcase_13 AC 1 ms
4,360 KB
testcase_14 AC 2 ms
4,356 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,360 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 1 ms
4,356 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 1 ms
4,356 KB
testcase_23 AC 2 ms
4,356 KB
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 3 ms
4,356 KB
testcase_27 AC 1,312 ms
4,636 KB
testcase_28 AC 2,263 ms
4,612 KB
testcase_29 AC 2,281 ms
4,648 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 1 ms
4,356 KB
testcase_32 AC 221 ms
19,992 KB
testcase_33 AC 219 ms
20,880 KB
testcase_34 AC 217 ms
20,032 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 1 ms
4,356 KB
testcase_38 AC 1 ms
4,352 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,356 KB
testcase_41 AC 2 ms
4,352 KB
testcase_42 AC 2 ms
4,356 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,356 KB
testcase_45 AC 2 ms
4,356 KB
testcase_46 AC 2 ms
4,360 KB
testcase_47 AC 1 ms
4,356 KB
testcase_48 AC 1 ms
4,356 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 3 ms
4,356 KB
testcase_51 AC 443 ms
4,664 KB
testcase_52 AC 110 ms
4,596 KB
testcase_53 AC 57 ms
5,568 KB
testcase_54 AC 57 ms
5,568 KB
testcase_55 AC 54 ms
5,576 KB
testcase_56 AC 56 ms
5,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap
import std.bigint;    // BigInt
import std.regex;     // Regex

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
T[] readArray(T)(size_t n){auto a=new T[](n),r=readln.splitter;foreach(ref v;a){v=r.front.to!T;r.popFront;}return a;}
T[] readArrayM(T)(size_t n){auto a=new T[](n);foreach(ref v;a)v=readln.chomp.to!T;return a;}

const inf = BigInt(1L<<62);

void main()
{
  auto s = readArray!long(26);
  string t; readV(t);

  auto y = new int[][](26);
  foreach (i; 0..26) {
    auto c = cast(char)('a'+i);
    y[i] = t.matchAll(regex(c.to!string ~ '+')).map!((m) => m[0].length.to!int).array;
    if (s[i] < y[i].sum) {
      writeln(0);
      return;
    }
  }

  auto d = BigInt(1);
  foreach (i, si; s) {
    d = calc(BigInt(si), t, y[i], d);
    if (d >= inf) {
      writeln("hel");
      return;
    }
  }

  writeln(d);
}

auto calc(BigInt s, string t, int[] y, BigInt d)
{
  auto ny = y.length.to!int;
  if (y.empty) return d;

  if (s > 10^^7) {
    if (ny == 1) {
      if (y[0] == 1) return d*s;
      if (y[0] == 2) return d*s*(s-1)/2;
    } else if (ny == 2) {
      if (y[0] == 1 && y[1] == 1) return d*s/2*(s-s/2);
    }
    return inf;
  }

  auto r = new Rat[](ny);
  foreach (i, yi; y)
    r[i] = Rat(yi+1, yi);

  auto h = heapify(r);
  foreach (i; 0..(s-y.sum).to!int) {
    auto ri = h.front;
    d = d * ri.n / (ri.n-ri.r);
    if (d >= inf) return inf;
    h.replaceFront(Rat(ri.n+1, ri.r));
  }

  return d;
}

struct Rat
{
  long n, r;

  auto opCmp(Rat b)
  {
    auto num = n, den = n-r, bnum = b.n, bden = b.n-b.r;
    auto c = num*bden - den*bnum;
    return c == 0 ? 0 : c < 0 ? -1 : 1;
  }
}
0