結果
問題 |
No.8024 等式
|
ユーザー |
|
提出日時 | 2017-07-09 14:47:50 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 706 ms / 5,000 ms |
コード長 | 3,446 bytes |
コンパイル時間 | 1,041 ms |
コンパイル使用メモリ | 117,388 KB |
実行使用メモリ | 44,544 KB |
最終ジャッジ日時 | 2024-06-30 04:13:56 |
合計ジャッジ時間 | 2,481 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#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) // 約分 void reduce(){ if(d < 0){ n *= -1; d *= -1; } long long a = abs(n); long long b = d; while(b != 0){ long long tmp = a % b; a = b; b = tmp; } n /= a; d /= a; } 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; reduce(); } 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> > memo; bool solve(const vector<Fraction>& v, Fraction target) { if(memo.find(v) != memo.end()) return false; memo.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){ if(v[i] < v[j]) a = v[j] - v[i]; else a = v[i] - v[j]; } 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 == target) return true; vector<Fraction> v2 = v; v2[i] = a; v2.erase(v2.begin() + j); sort(v2.begin(), v2.end()); if(solve(v2, target)) 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; } for(int i=1; i<n; ++i){ memo.clear(); vector<Fraction> v2(v.begin(), v.begin()+i); if(solve(v2, v[i])){ cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }