結果

問題 No.10 +か×か
ユーザー kimiyukikimiyuki
提出日時 2017-01-08 14:14:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,454 ms / 5,000 ms
コード長 1,424 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 88,792 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 06:21:30
合計ジャッジ時間 4,097 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 32 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 35 ms
4,376 KB
testcase_07 AC 2,454 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= int(m); --(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using namespace std;
int main() {
    int n, total; cin >> n >> total;
    vector<int> a(n); repeat (i,n) cin >> a[i];
    function<int (int, int)> upper_bound = [&](int i, int acc) { return i == n or acc == total+1 ? acc : upper_bound(i+1, min(total+1, max(acc + a[i], acc * a[i]))); };
    string result;
    function<bool (int, int)> go = [&](int i, int acc) {
        if (i == n) return acc == total;
        int r = upper_bound(i, acc);
        if (acc <= total and total <= r) {
            result.push_back('+');
            if (go(i+1, acc + a[i])) return true;
            result.pop_back();
            result.push_back('*');
            if (go(i+1, acc * a[i])) return true;
            result.pop_back();
        }
        return false;
    };
    go(1, a[0]);
    cout << result << endl;
    return 0;
}
0