結果

問題 No.751 Frac #2
ユーザー gazelle
提出日時 2018-11-09 22:26:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 1,323 bytes
コンパイル時間 1,252 ms
コンパイル使用メモリ 105,096 KB
最終ジャッジ日時 2025-01-06 16:08:08
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<iomanip>
#include<math.h>
#include<bitset>
#include<cassert>
using namespace std;
using ll=long long;
using ld=long double;
using P=pair<ll,ll>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i,n,m) for(ll i=n;i<(ll)m;i++)
#define REP(i,n) FOR(i,0,n)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ALL(v) v.begin(),v.end()
#define UNIQUE(v)  sort(ALL(v));v.erase(unique(ALL(v)),v.end());
#define pb push_back

ll gcd(ll a, ll b) {		// ユークリッドの互除法
	if (b == 0) return a;
	return gcd(b, a % b);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n1;
	cin >> n1;
	vector<ll> a(n1);
	REP(i,n1) cin >> a[i];
	if(n1 == 1) {
		n1++;
		a.pb(1);
	}
	ll n2;
	cin >> n2;
	vector<ll> b(n2);
	REP(i,n2) cin >> b[i];
	if(n2 == 1) {
		n2++;
		b.pb(1);
	}
	reverse(ALL(a));
	while(n1 > 2) {
		a[n1 - 3] = a[n1 - 3] * a[n1 - 2];
		a[n1 - 2] = a[n1 - 1];
		n1--;
	}
	while(n2 > 2) {
		b[n2 - 3] = b[n2 - 3] * b[n2 - 1];
		n2--;
	}
	ll up = a[1] * b[1], dw = a[0] * b[0];
	bool sign = (up > 0) != (dw > 0);
	up = abs(up);
	dw = abs(dw);
	ll g = gcd(up, dw);
	if(sign) cout << "-";
	cout << up / g << " " << dw / g << endl;
	return 0;
}
0