結果

問題 No.751 Frac #2
ユーザー mamekinmamekin
提出日時 2019-01-02 19:46:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 3,699 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 104,356 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-13 17:16:10
合計ジャッジ時間 2,182 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,504 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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 <array>
#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;

template <class T1>
class Operators
{
public:
    template <class T2>
    const T1 operator+(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans += right;
        return ans;
    }
    template <class T2>
    const T1 operator-(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans -= right;
        return ans;
    }
    template <class T2>
    const T1 operator*(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans *= right;
        return ans;
    }
    template <class T2>
    const T1 operator/(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans /= right;
        return ans;
    }
    bool operator!=(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return !(left == right);
    }
    bool operator>(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return right < left;
    }
    bool operator<=(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return !(right < left);
    }
    bool operator>=(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return !(left < right);
    }
};

class Fraction : public Operators<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);
    }
    Fraction& operator+=(const Fraction& f){
        n = n * f.d + d * f.n;
        d *= f.d;
        reduce();
        return *this;
    }
    Fraction& operator-=(const Fraction& f){
        n = n * f.d - d * f.n;
        d *= f.d;
        reduce();
        return *this;
    }
    Fraction& operator*=(const Fraction& f){
        n *= f.n;
        d *= f.d;
        reduce();
        return *this;
    }
    Fraction& operator/=(const Fraction& f){
        n *= f.d;
        d *= f.n;
        reduce();
        return *this;
    }
    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;
    }
};

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

    int m;
    cin >> m;
    vector<int> b(m);
    for(int i=0; i<m; ++i)
        cin >> b[i];

    Fraction x = a[0];
    for(int i=1; i<n; ++i)
        x /= a[i];

    Fraction y = b[m-1];
    for(int i=m-2; i>=0; --i)
        y = Fraction(b[i]) / y;

    Fraction ans = x / y;
    cout << ans.getValue().first << ' ' << ans.getValue().second << endl;

    return 0;
}
0