結果

問題 No.3024 等式
ユーザー mamekinmamekin
提出日時 2017-07-09 13:25:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,925 bytes
コンパイル時間 1,138 ms
コンパイル使用メモリ 114,248 KB
実行使用メモリ 238,084 KB
最終ジャッジ日時 2023-07-29 03:36:47
合計ジャッジ時間 11,684 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
238,084 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 12 ms
4,380 KB
testcase_07 AC 13 ms
4,820 KB
testcase_08 AC 20 ms
5,624 KB
testcase_09 AC 8 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 738 ms
38,544 KB
testcase_13 AC 713 ms
39,636 KB
testcase_14 AC 1,280 ms
60,788 KB
testcase_15 AC 22 ms
5,564 KB
testcase_16 AC 31 ms
6,356 KB
testcase_17 AC 102 ms
11,788 KB
testcase_18 AC 308 ms
22,572 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 27 ms
6,120 KB
testcase_21 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

class Fraction
{
private:
    long long n; // 分子(numerator)
    long long d; // 分母(denominator)
public:
    Fraction(){
        n = 0;
        d = 1;
    }
    Fraction(long long n0){
        n = n0;
        d = 1;
    }
    Fraction(long long n0, long long d0){
        n = n0;
        d = d0;
    }
    pair<long long, long long> getValue() const{
        return make_pair(n, d);
    }
    const Fraction operator+(const Fraction& f) const{
        return Fraction(n*f.d + d*f.n, d*f.d);
    }
    const Fraction operator-(const Fraction& f) const{
        return Fraction(n*f.d - d*f.n, d*f.d);
    }
    const Fraction operator*(const Fraction& f) const{
        return Fraction(n*f.n, d*f.d);
    }
    const Fraction operator/(const Fraction& f) const{
        return Fraction(n*f.d, d*f.n);
    }
    bool operator==(const Fraction& f) const{
        return n == f.n && d == f.d;
    }
    bool operator!=(const Fraction& f) const{
        return n != f.n || d != f.d;
    }
    bool operator<(const Fraction& f) const{
        return n * f.d < f.n * d;
    }
};

set<vector<Fraction> > history;

bool solve(const vector<Fraction>& v)
{
    if(history.find(v) != history.end())
        return false;
    history.insert(v);

    int n = v.size();
    for(int i=0; i<n; ++i){
        for(int j=0; j<i; ++j){
            for(int k=0; k<5; ++k){
                Fraction a;
                if(k == 0){
                    a = v[i] + v[j];
                }
                else if(k == 1){
                    a = v[i] - v[j];
                    if(a < 0)
                        a = v[j] - v[i];
                }
                else if(k == 2){
                    a = v[i] * v[j];
                }
                else if(k == 3){
                    a = v[i] / v[j];
                }
                else{
                    a = v[j] / v[i];
                }

                if(a == 0)
                    return true;

                vector<Fraction> v2 = v;
                v2[i] = a;
                v2.erase(v2.begin() + j);
                sort(v2.begin(), v2.end());
                if(solve(v2))
                    return true;
            }
        }
    }

    return false;
}

int main()
{
    int n;
    cin >> n;

    vector<Fraction> v(n);
    for(int i=0; i<n; ++i){
        int a;
        cin >> a;
        v[i] = a;
    }

    if(solve(v))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;

    return 0;
}
0