結果

問題 No.2271 平方根の13桁精度近似計算
ユーザー jinya nakamurajinya nakamura
提出日時 2023-04-15 10:37:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,428 bytes
コンパイル時間 2,079 ms
コンパイル使用メモリ 198,552 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-19 06:19:34
合計ジャッジ時間 3,554 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,948 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef unsigned long long ull;
// const ll INF = numeric_limits<ll>::max() / 4;
// const int INF = numeric_limits<int>::max() / 4;
// cout << std::fixed << std::setprecision(15);


int main() {
	ll N; int E;
	cin >> N >> E;

	int K = 20;
	ll EPS = 1;
	for(int i = 0; i < K; i++) EPS *= 5;
	if(N < 0) N += EPS;

	ll D = 0;
	while(N % 5 == 0){
		N /= 5;
		D++;
		if(E - D <= 0){
			cout << 0 << endl;
			return 0;
		}
	}
	if(D % 2 == 1){
		cout << "NaN" << endl;
		return 0;
	}

	vector<ll> T(K, 0);
	for(int k = 0; k < K; k++){
		T[k] = N % 5LL;
		N /= 5;
	}


	vector<ll> R(K, 0);
	vector<ll> U(K, 0);
	for(int e = 0; e < E - D; e++){
		ll l = U[e];
		U[e] = 0;
		for(int j = 1; j <= e - 1; j++){
			l += R[j] * R[e - j];
		}
		bool f = false;
		for(R[e] = 0; R[e] < 5; R[e]++){
			ll s = R[e] * R[0] * 2;
			if(e == 0) s = R[0] * R[0];
			if((l + s) % 5 == T[e]){
				l += s;
				l /= 5;
				for(int j = e + 1; j < K; j++){
					U[j] += l % 5;
					l /= 5;
				}
				f = true;
				break;
			}
		}
		if(!f){
			cout << "NaN" << endl;
			return 0;
		}
	}

	ll r = 0;
	ll d = 1;
	for(int k = 0; k <= K; k++){
		r += R[k] * d;
		d *= 5LL;
	}
	for(int i = 0; i < D / 2; i++) r *= 5;

	EPS = 1;
	for(int i = 0; i < E; i++) EPS *= 5;
	while((1LL << 29) < r) r -= EPS;
	if(r < -(1LL << 29)){
		cout << "NaN" << endl;
	}else{
		cout << r << endl;
	}

	return 0;
}
0