結果

問題 No.10 +か×か
ユーザー kimiyukikimiyuki
提出日時 2017-01-08 14:10:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4,196 ms / 5,000 ms
コード長 2,080 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 89,776 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 06:21:36
合計ジャッジ時間 6,001 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 59 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 60 ms
4,380 KB
testcase_07 AC 4,196 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 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)
#define debug(x) #x << " = " << (x) << " "
using namespace std;
template <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }
template <class T> void setmin(T & a, T const & b) { if (b < a) a = b; }
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }
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]))); };
    function<int (int, int)> lower_bound = [&](int i, int acc) { return i == n or acc == total+1 ? acc : lower_bound(i+1, min(total+1, min(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);
        int l = lower_bound(i, acc);
        if (l <= 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