結果

問題 No.1936 Rational Approximation
ユーザー 👑 KazunKazun
提出日時 2022-05-13 22:22:01
言語 C++17
(gcc 12.2.0 + boost 1.81.0)
結果
AC  
実行時間 1,318 ms / 2,000 ms
コード長 771 bytes
コンパイル時間 1,855 ms
実行使用メモリ 6,324 KB
最終ジャッジ日時 2023-02-21 15:14:43
合計ジャッジ時間 19,083 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,087 ms
6,208 KB
testcase_01 AC 1,039 ms
6,316 KB
testcase_02 AC 1,318 ms
6,252 KB
testcase_03 AC 945 ms
6,208 KB
testcase_04 AC 944 ms
6,212 KB
testcase_05 AC 943 ms
6,324 KB
testcase_06 AC 944 ms
6,180 KB
testcase_07 AC 943 ms
6,204 KB
testcase_08 AC 942 ms
6,268 KB
testcase_09 AC 945 ms
6,208 KB
testcase_10 AC 941 ms
6,176 KB
testcase_11 AC 941 ms
6,196 KB
testcase_12 AC 941 ms
6,280 KB
testcase_13 AC 941 ms
6,196 KB
testcase_14 AC 940 ms
6,268 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