結果

問題 No.1633 Sorting Integers (Multiple of 2^K)
ユーザー qwewe
提出日時 2025-04-24 12:30:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,570 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 82,884 KB
実行使用メモリ 54,800 KB
最終ジャッジ日時 2025-04-24 12:31:39
合計ジャッジ時間 3,566 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
c = list(map(int, input().split()))

max_m = -1

# Check for 2^m numbers with exactly n digits
lower = 10 ** (n-1)
upper = 10 ** n
current = 1  # 2^0 is 1, but we need to find m where 2^m has n digits
m = 0
found = False
while True:
    current = 1 << m  # equivalent to 2^m
    if current >= upper:
        break
    if current >= lower:
        s = str(current)
        if len(s) != n:
            m += 1
            continue
        # Check digit counts
        freq = [0] * 9
        for ch in s:
            d = int(ch)
            freq[d - 1] += 1
        valid = True
        for i in range(9):
            if freq[i] != c[i]:
                valid = False
                break
        if valid:
            if m > max_m:
                max_m = m
    m += 1

max_k = 0

# Check for k from N down to 0
for k in range(n, 0, -1):
    if k > 3:
        continue
    if k == 1:
        # Check if any even digit exists
        has_even = False
        for i in range(9):
            if (i + 1) % 2 == 0 and c[i] > 0:
                has_even = True
                break
        if has_even:
            # Check remaining digits sum to n - k
            remaining = sum(c) - 1
            if remaining == n - 1:
                max_k = 1
                break
    elif k == 2:
        # Check if any two digits form a number divisible by 4
        found = False
        for d1 in range(1, 10):
            if c[d1 - 1] == 0:
                continue
            for d2 in range(1, 10):
                if d1 == d2:
                    if c[d1 - 1] < 2:
                        continue
                else:
                    if c[d2 - 1] < 1:
                        continue
                # Check if the number is divisible by 4
                num = d1 * 10 + d2
                if num % 4 == 0:
                    # Check remaining counts
                    temp_c = c.copy()
                    temp_c[d1 - 1] -= 1
                    temp_c[d2 - 1] -= 1
                    if sum(temp_c) == n - 2 and all(x >= 0 for x in temp_c):
                        found = True
                        break
            if found:
                break
        if found:
            max_k = 2
            break
    elif k == 3:
        # Check if any three digits form a number divisible by 8
        found = False
        for d1 in range(1, 10):
            if c[d1 - 1] == 0:
                continue
            temp_c1 = c.copy()
            temp_c1[d1 - 1] -= 1
            if temp_c1[d1 - 1] < 0:
                continue
            for d2 in range(1, 10):
                if temp_c1[d2 - 1] == 0:
                    continue
                temp_c2 = temp_c1.copy()
                temp_c2[d2 - 1] -= 1
                if temp_c2[d2 - 1] < 0:
                    continue
                for d3 in range(1, 10):
                    if temp_c2[d3 - 1] == 0:
                        continue
                    temp_c3 = temp_c2.copy()
                    temp_c3[d3 - 1] -= 1
                    if temp_c3[d3 - 1] < 0:
                        continue
                    num = d1 * 100 + d2 * 10 + d3
                    if num % 8 == 0:
                        if sum(temp_c3) == n - 3 and all(x >= 0 for x in temp_c3):
                            found = True
                            break
                if found:
                    break
            if found:
                break
        if found:
            max_k = 3
            break

# Determine the answer
answer = max(max_m, max_k)
print(answer if answer != -1 else 0)
0