結果

問題 No.1936 Rational Approximation
ユーザー AuroraAurora
提出日時 2022-05-13 22:33:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 704 bytes
コンパイル時間 1,510 ms
コンパイル使用メモリ 165,240 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 08:03:55
合計ジャッジ時間 2,427 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 WA -
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}

int main(){
  long long int P,Q;
  cin >> P >> Q;
  long long int x1,y1;
  extGCD(Q,P,x1,y1);
  y1 *= -1;
  long long int t1 = (Q-y1)/Q;
  long long int Rp = P+x1*t1;
  long long int Rq = Q+y1*t1;
  if(P == Q-1){
    Rp = 1;
    Rq = 1;
  }
  long long int x2,y2;
  extGCD(P,Q,y2,x2);
  x2 *= -1;
  while(x2 <= 0 || y2 <= 0){
    x2 += P;
    y2 += Q;
  }
  long long int Lp = x2;
  long long int Lq = y2;
  cout << Lp+Lq+Rp+Rq << endl;
}
0