結果

問題 No.247 線形計画問題もどき
ユーザー rpy3cpprpy3cpp
提出日時 2015-07-17 22:58:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 862 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 10,840 KB
実行使用メモリ 8,224 KB
最終ジャッジ日時 2023-09-21 17:21:46
合計ジャッジ時間 1,969 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
7,820 KB
testcase_01 AC 16 ms
7,700 KB
testcase_02 AC 16 ms
7,836 KB
testcase_03 AC 16 ms
7,780 KB
testcase_04 AC 16 ms
7,836 KB
testcase_05 AC 16 ms
7,784 KB
testcase_06 AC 16 ms
7,816 KB
testcase_07 AC 16 ms
7,700 KB
testcase_08 AC 16 ms
7,844 KB
testcase_09 AC 16 ms
7,848 KB
testcase_10 AC 16 ms
7,700 KB
testcase_11 AC 16 ms
7,836 KB
testcase_12 AC 16 ms
7,864 KB
testcase_13 AC 16 ms
7,772 KB
testcase_14 AC 16 ms
7,716 KB
testcase_15 AC 16 ms
7,784 KB
testcase_16 AC 16 ms
7,756 KB
testcase_17 AC 18 ms
7,780 KB
testcase_18 AC 16 ms
7,728 KB
testcase_19 AC 30 ms
8,104 KB
testcase_20 AC 18 ms
8,180 KB
testcase_21 AC 17 ms
8,100 KB
testcase_22 AC 17 ms
8,152 KB
testcase_23 AC 16 ms
8,152 KB
testcase_24 AC 36 ms
8,224 KB
testcase_25 AC 26 ms
8,132 KB
testcase_26 AC 17 ms
7,788 KB
testcase_27 AC 23 ms
7,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    C = int(input())
    N = int(input())
    As = list(map(int, input().split()))
    return C, N, As

def solve(C, N, As):
    As.sort()
    lower, residue = divmod(C, As[-1])
    if residue == 0:
        return lower
    lower += 1
    upper = C // As[0]
    for total in range(lower, upper + 1):
        if has_solution(total, C, As, N-1):
            return total
    return -1

def has_solution(total, C, As, idx):
    a = As[idx]
    if idx == 0:
        n, r = divmod(C, a)
        return r == 0 and n == total
    for n in range(C//a, -1, -1):
        new_total = total - n
        new_C = C - n * a
        if new_C > new_total * As[idx - 1]:
            return False
        result = has_solution(new_total, new_C, As, idx - 1)
        if result:
            return True
    return False

C, N, As = read_data()
print(solve(C, N, As))
0