結果

問題 No.1936 Rational Approximation
ユーザー 👑 KazunKazun
提出日時 2022-05-13 22:22:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,415 ms / 2,000 ms
コード長 771 bytes
コンパイル時間 1,978 ms
コンパイル使用メモリ 199,960 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 07:48:13
合計ジャッジ時間 18,802 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

//参考元
//https://tjkendev.github.io/procon-library/python/math/stern-brocot-tree.html

vector<ll> Stern_Brocot(ll p, ll q, ll n){
    ll a=0,b=1,c=1,d=0;
    bool lu=true,ru=true;
    ll lx=0,ly=1,rx=1,ry=0;

    ll times=1000000010;
    ll s,t;
    while (times){
    	times--;
    	s=a+c; t=b+d;
    	if (t*p<s*q){
    		c=s; d=t;
    		if (t<=n){
    			rx=s; ry=t;
    		}else{
    			ru=false;
    		}
    	}
    	
    	if (t*p>s*q){
    		a=s; b=t;
    		if (t<=n){
    			lx=s; ly=t;
    		}else{
    			ru=false;
    		}
    	}
    }
        
    return vector<ll>{lx,ly,rx,ry};
}

int main(){
	ll P,Q;
	cin >> P >> Q;
	vector<ll> v=Stern_Brocot(P,Q,Q);
	cout << v[0]+v[1]+v[2]+v[3] << endl;
}
0