結果

問題 No.3024 等式
ユーザー kimiyukikimiyuki
提出日時 2017-04-01 00:45:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,353 bytes
コンパイル時間 1,030 ms
コンパイル使用メモリ 80,548 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-21 21:08:49
合計ジャッジ時間 12,990 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
4,376 KB
testcase_01 AC 30 ms
4,376 KB
testcase_02 AC 174 ms
4,380 KB
testcase_03 AC 178 ms
4,376 KB
testcase_04 AC 119 ms
4,376 KB
testcase_05 AC 123 ms
4,376 KB
testcase_06 AC 998 ms
4,376 KB
testcase_07 AC 1,056 ms
4,376 KB
testcase_08 AC 1,206 ms
4,376 KB
testcase_09 AC 349 ms
4,376 KB
testcase_10 AC 352 ms
4,380 KB
testcase_11 AC 367 ms
4,376 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
#include <array>
#include <stack>
#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 ll = long long;
using namespace std;
template <class T> inline void setmax(T & a, T const & b) { a = max(a, b); }
template <class T> inline void setmin(T & a, T const & b) { a = min(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); }
ll gcd(ll a, ll b) { while (a) { b %= a; swap(a, b); } return b; }
pair<ll, ll> make_rational(ll num, ll den) {
    ll d = gcd(num, den);
    return make_pair(num / d, den / d);
}
pair<ll, ll> operator + (pair<ll, ll> x, pair<ll, ll> y) {
    return make_rational(x.first * y.second + y.first * x.second, x.second * y.second);
}
pair<ll, ll> operator - (pair<ll, ll> x, pair<ll, ll> y) {
    return make_rational(x.first * y.second - y.first * x.second, x.second * y.second);
}
pair<ll, ll> operator * (pair<ll, ll> x, pair<ll, ll> y) {
    return make_rational(x.first * y.first, x.second * y.second);
}
pair<ll, ll> operator / (pair<ll, ll> x, pair<ll, ll> y) {
    return make_rational(x.first * y.second, x.second * y.first);
}
constexpr int max_n = 7;
int main() {
    int n; scanf("%d", &n);
    vector<int> a(n); repeat (i,n) scanf("%d", &a[i]);
    bool found = false;
    array<pair<ll, ll>, 2*max_n-1> s;
    repeat (i,n) s[i] = { ll(a[i]), 1 };
    repeat_from (i,n,max_n) s[i] = { -1, 0 };
    repeat(i,max_n-1) s[max_n+i] = { -2, 0 };
    whole(sort, s);
    do {
        stack<pair<ll,ll> > stk;
        int i = 0;
        function<void ()> go = [&]() {
            if (found) return;
            if (s[i] == make_pair<ll, ll>(-1, 0)) {
                // invalid
            } else if (s[i] == make_pair<ll, ll>(-2, 0)) { // op
                if (stk.size() >= 2) {
                    pair<ll,ll> y = stk.top(); stk.pop();
                    pair<ll,ll> x = stk.top(); stk.pop();
                    if (x == y) {
                        found = true;
                    }
                    ++ i;
                    stk.push(x + y); go(); stk.pop();
                    stk.push(x - y); go(); stk.pop();
                    stk.push(x * y); go(); stk.pop();
                    stk.push(x / y); go(); stk.pop();
                    -- i;
                    stk.push(x);
                    stk.push(y);
                }
            } else {
                stk.push(s[i]);
                ++ i;
                go();
                -- i;
                stk.pop();
            }
        };
        go();
    } while(whole(next_permutation, s));
    printf("%s\n", found ? "YES" : "NO");
    return 0;
}
0