結果

問題 No.603 hel__world (2)
ユーザー zimphazimpha
提出日時 2017-12-03 03:18:03
言語 Python2
(2.7.18)
結果
AC  
実行時間 919 ms / 3,000 ms
コード長 2,642 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 7,168 KB
実行使用メモリ 71,296 KB
最終ジャッジ日時 2024-05-07 23:50:29
合計ジャッジ時間 26,532 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 614 ms
69,888 KB
testcase_01 AC 621 ms
69,632 KB
testcase_02 AC 635 ms
69,888 KB
testcase_03 AC 632 ms
69,760 KB
testcase_04 AC 647 ms
69,760 KB
testcase_05 AC 654 ms
69,888 KB
testcase_06 AC 654 ms
69,760 KB
testcase_07 AC 653 ms
69,760 KB
testcase_08 AC 644 ms
69,828 KB
testcase_09 AC 631 ms
69,760 KB
testcase_10 AC 638 ms
69,884 KB
testcase_11 AC 900 ms
71,040 KB
testcase_12 AC 918 ms
70,912 KB
testcase_13 AC 919 ms
70,784 KB
testcase_14 AC 634 ms
69,888 KB
testcase_15 AC 629 ms
69,760 KB
testcase_16 AC 630 ms
69,888 KB
testcase_17 AC 630 ms
69,760 KB
testcase_18 AC 638 ms
69,888 KB
testcase_19 AC 629 ms
69,760 KB
testcase_20 AC 869 ms
71,296 KB
testcase_21 AC 847 ms
71,296 KB
testcase_22 AC 646 ms
69,888 KB
testcase_23 AC 635 ms
69,760 KB
testcase_24 AC 628 ms
69,888 KB
testcase_25 AC 914 ms
71,024 KB
testcase_26 AC 913 ms
71,040 KB
testcase_27 AC 915 ms
70,784 KB
testcase_28 AC 891 ms
70,976 KB
testcase_29 AC 762 ms
71,040 KB
testcase_30 AC 683 ms
70,528 KB
testcase_31 AC 619 ms
69,632 KB
testcase_32 AC 775 ms
70,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10 ** 6 + 3
denom = 10 ** 40
fac = [1] * mod
inv = [1] * mod

for i in xrange(2, mod):
    fac[i] = i * fac[i - 1] % mod
    inv[i] = (mod - mod / i) * inv[mod % i] % mod

def solve():
    def binom(n, m):
        if n < m: return 0
        if n == m or m == 0: return 1
        nq, nr = divmod(n, mod)
        mq, mr = divmod(m, mod)
        if nr < mr: return 0
        return fac[nr] * inv[fac[mr]] * inv[fac[nr - mr]] * binom(nq, mq) % mod

    counts = map(int, raw_input().split())
    s = raw_input()
    n = len(s)
    lens = []
    for i in xrange(26):
        lens.append({})
    freqs = [0] * 26
    last, cnt = -1, 0
    for c in s:
        i = ord(c) - ord('a')
        if last == i:
            cnt += 1
        else:
            if cnt:
                if cnt in lens[last]:
                    lens[last][cnt] += 1
                else:
                    lens[last][cnt] = 1
            cnt = 1
        freqs[i] += 1
        last = i
    if cnt in lens[last]:
        lens[last][cnt] += 1
    else:
        lens[last][cnt] = 1
    lens = [x.items() for x in lens]
    ans = 1
    for i in xrange(26):
        if counts[i] < freqs[i]:
            return 0
        if freqs[i] == 0 or counts[i] == freqs[i]:
            continue
        if len(lens[i]) == 1 and lens[i][0][1] == 1:
            ans = ans * binom(counts[i], lens[i][0][0]) % mod
        else:
            rest = counts[i] - freqs[i]
            ok = denom + 1
            ng = n * denom + 1
            exact = False
            while ng - ok > 1:
                mid = (ok + ng) >> 1
                total = 0
                for l, c in lens[i]:
                    total += (l * denom) / (mid - denom) * c
                if total >= rest:
                    ok = mid
                    if total == rest:
                        exact = True
                        ng = mid
                else:
                    ng = mid
            for l, c in lens[i]:
                k = (l * denom) / (ng - denom)
                if k:
                    ans = ans * pow(binom(l + k, l) % mod, c, mod) % mod
                    rest -= k * c
            assert rest >= 0
            if not exact:
                for l, c in lens[i]:
                    k1 = (l * denom) / (ok - denom)
                    k2 = (l * denom) / (ng - denom)
                    if k1 > k2:
                        assert k1 == k2 + 1
                        ans = ans * pow((k1 + l) * inv[k1 % mod] % mod, min(rest, c), mod) % mod
                        rest -= min(rest, c)
                        if rest == 0:
                            break
    return ans

print solve()
0