結果

問題 No.751 Frac #2
ユーザー Y17Y17
提出日時 2018-11-09 22:15:56
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,208 bytes
コンパイル時間 2,056 ms
使用メモリ 3,544 KB
最終ジャッジ日時 2022-12-26 20:11:52
合計ジャッジ時間 2,731 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long gcd(long long a, long long b){
  if(a < b){
    swap(a, b);
  }

  while(1){
    if(b == 0) return a;
    long long tmp = a % b;
    a = b;
    b = tmp;
  }
}



int main(){
  long long n1, n2;
  long long a;
  pair<long long, long long> bun1, bun2 , ans;

  cin >> n1 >> a;

  bun1.first = a;
  bun1.second = 1;

  for(long long i = 1;i < n1;i++){
    cin >> a;
    bun1.second *= a;
    long long tmp = gcd(bun1.first, bun1.second);
    bun1.first /= tmp;
    bun1.second /= tmp;
  }



  cin >> n2;

  long long b[20];
  for(long long i = 0;i < n2;i++){
    cin >> b[i];
  }

  bun2.first = b[n2-1];
  bun2.second = 1;

  for(long long i = n2-2;i >= 0;i--){
    long long x = bun2.first;
    long long y = bun2.second;

    bun2.first = b[i] * y;
    bun2.second = x;

    long long tmp = __gcd(bun2.first, bun2.second);
    bun2.first /= tmp;
    bun2.second /= tmp;
  }

  ans.first = bun1.first * bun2.second;
  ans.second = bun1.second * bun2.first;

  long long tmp = gcd(ans.first, ans.second);

  if(ans.second/tmp < 0){
    ans.first *= -1;
    ans.second *= -1;
  }

  cout << ans.first/tmp << " " << ans.second/tmp << endl;

  return 0;
}
0