結果

問題 No.2593 Reorder and Mod 120
ユーザー 👑 rin204rin204
提出日時 2023-12-21 20:40:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 81,956 KB
最終ジャッジ日時 2023-12-21 20:40:35
合計ジャッジ時間 2,376 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,332 KB
testcase_01 AC 36 ms
53,332 KB
testcase_02 AC 35 ms
53,332 KB
testcase_03 AC 35 ms
53,332 KB
testcase_04 AC 34 ms
53,332 KB
testcase_05 AC 34 ms
53,332 KB
testcase_06 AC 34 ms
53,332 KB
testcase_07 AC 34 ms
53,332 KB
testcase_08 AC 34 ms
53,332 KB
testcase_09 AC 34 ms
53,332 KB
testcase_10 AC 35 ms
53,332 KB
testcase_11 AC 41 ms
59,836 KB
testcase_12 AC 40 ms
62,352 KB
testcase_13 AC 57 ms
80,444 KB
testcase_14 AC 46 ms
67,992 KB
testcase_15 AC 52 ms
75,440 KB
testcase_16 AC 40 ms
62,240 KB
testcase_17 AC 46 ms
67,432 KB
testcase_18 AC 43 ms
64,776 KB
testcase_19 AC 56 ms
80,336 KB
testcase_20 AC 59 ms
81,956 KB
testcase_21 AC 59 ms
81,956 KB
testcase_22 AC 61 ms
81,956 KB
testcase_23 AC 61 ms
81,956 KB
testcase_24 AC 58 ms
81,828 KB
testcase_25 AC 58 ms
81,828 KB
testcase_26 AC 58 ms
81,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def next_permutation(P):
    n = len(P)
    for i in range(n - 2, -1, -1):
        if P[i] < P[i + 1]:
            l = i + 1
            r = n - 1
            while r > l:
                P[l], P[r] = P[r], P[l]
                l += 1
                r -= 1
            for j in range(i + 1, n):
                if P[i] < P[j]:
                    P[i], P[j] = P[j], P[i]
                    return True
    return False


def all_permutations(P):
    # 全列挙したい場合はソートしてある状態で渡す
    yield P
    while next_permutation(P):
        yield P


def prev_permutation(P):
    n = len(P)
    for i in range(n - 2, -1, -1):
        if P[i] > P[i + 1]:
            l = i + 1
            r = n - 1
            while r > l:
                P[l], P[r] = P[r], P[l]
                l += 1
                r -= 1
            for j in range(i + 1, n):
                if P[i] > P[j]:
                    P[i], P[j] = P[j], P[i]
                    return True
    return False


def rev_all_permutations(P):
    # 全列挙したい場合は逆順ソートしてある状態で渡す
    yield P
    while prev_permutation(P):
        yield P


n = int(input())
S = list(map(int, input()))
if n <= 3:
    tf = [False] * 120
    S.sort()
    for P in all_permutations(S):
        tot = 0
        for p in P:
            tot = tot * 10 + p
        tf[tot % 120] = True
    print(sum(tf))
else:
    cnt = [0] * 10
    for s in S:
        cnt[s] += 1
    tf = [False] * 40
    for i in range(1, 10):
        if cnt[i] == 0:
            continue
        cnt[i] -= 1
        for j in range(1, 10):
            if cnt[j] == 0:
                continue
            cnt[j] -= 1
            for k in range(1, 10):
                if cnt[k] == 0:
                    continue
                x = i * 100 + j * 10 + k
                tf[x % 40] = True
            cnt[j] += 1
        cnt[i] += 1

    print(sum(tf))
0