結果

問題 No.346 チワワ数え上げ問題
ユーザー yomo3yomo3
提出日時 2020-02-29 23:52:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 824 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2024-04-21 21:58:16
合計ジャッジ時間 2,714 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
10,752 KB
testcase_01 AC 34 ms
10,624 KB
testcase_02 AC 37 ms
10,624 KB
testcase_03 AC 34 ms
10,624 KB
testcase_04 AC 34 ms
10,496 KB
testcase_05 AC 33 ms
10,624 KB
testcase_06 AC 35 ms
10,624 KB
testcase_07 AC 34 ms
10,496 KB
testcase_08 AC 34 ms
10,624 KB
testcase_09 AC 35 ms
10,496 KB
testcase_10 AC 77 ms
14,464 KB
testcase_11 AC 60 ms
12,928 KB
testcase_12 AC 68 ms
13,696 KB
testcase_13 AC 56 ms
12,544 KB
testcase_14 AC 37 ms
10,624 KB
testcase_15 AC 65 ms
13,312 KB
testcase_16 AC 69 ms
13,824 KB
testcase_17 AC 75 ms
14,464 KB
testcase_18 AC 72 ms
14,592 KB
testcase_19 AC 68 ms
13,952 KB
testcase_20 AC 71 ms
11,776 KB
testcase_21 AC 180 ms
14,720 KB
testcase_22 AC 184 ms
14,720 KB
testcase_23 AC 34 ms
10,752 KB
testcase_24 AC 34 ms
10,624 KB
testcase_25 AC 33 ms
10,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import reduce

def comb(n, r):
    if n < 0 or r < 0 or r > n:
        return 0
    r = min(r, n - r)
    if r == 0: return 1

    numer = [n - r + i + 1 for i in range(r)]
    denom = [i + 1 for i in range(r)]

    for i in range(1, r):
        divisor = denom[i]
        if divisor > 1:
            offset = (n - r) % (i + 1)
            for j in range(i, r, i+1):
                numer[j - offset] //= divisor
                denom[j] //= divisor

    return reduce(lambda x, y: x*y if y > 1 else x, numer)

def main():
    S = input()
    N = len(S)
    cnt_w = [0] * N
    for i in range(N-1, -1, -1):
        cnt_w[i] = (0 if i == N-1 else cnt_w[i + 1]) + (1 if S[i] == 'w' else 0)
    ans = 0
    for i in range(N-1):
        if S[i] == 'c':
            ans += comb(cnt_w[i+1], 2)
    print(ans)
main()
0