結果

問題 No.25 有限小数
ユーザー femto
提出日時 2016-09-22 21:33:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 800 bytes
コンパイル時間 743 ms
コンパイル使用メモリ 78,912 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-17 13:12:46
合計ジャッジ時間 1,611 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;

typedef long long ll;
ll gcd(ll a, ll b) {
	if(b == 0) return a;
	return gcd(b, a % b);
}


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll N, M;
	cin >> N >> M;

	ll g = gcd(N, M);
	N /= g, M /= g;

	if(M == 1) {
		while(N % 10 == 0) N /= 10;
		cout << N % 10 << endl;
		return 0;
	}

	int t = 0, f = 0;
	ll m = M;
	while(m % 2 == 0) {
		m /= 2;
		t++;
	}
	while(m % 5 == 0) {
		m /= 5;
		f++;
	}

	if(m != 1) {
		cout << -1 << endl;
		return 0;
	}

	while(t != f) {
		if(t > f) {
			N *= 5;
			t--;
		}
		else {
			N *= 5;
			f--;
		}
		N %= 1000000;
		while(N % 10 == 0) N /= 10;
	}

	cout << N % 10 << endl;

}
0