結果

問題 No.25 有限小数
ユーザー warashiwarashi
提出日時 2017-10-01 15:33:10
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 810 bytes
コンパイル時間 9,084 ms
コンパイル使用メモリ 222,008 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 17:28:17
合計ジャッジ時間 10,327 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 0 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
5,376 KB
testcase_16 WA -
testcase_17 AC 1 ms
5,376 KB
testcase_18 WA -
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
)

func main() {
	var N, M uint64
	fmt.Scan(&N, &M)
	g := gcd(N, M)
	N /= g
	M /= g

	// 末尾の0を取り除く
	// ここでやっておかないとオーバーフローする
	for N%10 == 0 {
		N /= 10
	}

	// 関係あるのは1の位だけ
	N %= 10

	// 分母分子を[2,5]で割って、小数になってしまうので10かける
	// オーバーフロー対策に毎回末尾の0を取り除いた上で1の位だけにする
	c := true
	for c {
		switch {
		case N%10 == 0:
			N /= 10
		case M%2 == 0:
			M /= 2
			N *= 5
		case M%5 == 0:
			M /= 5
			N *= 2
		default:
			c = false
		}
	}
	if M != 1 {
		fmt.Println(-1)
		return
	}
	fmt.Println(N % 10)
}
func gcd(a, b uint64) uint64 {
	if b < a {
		a, b = b, a
	}
	if b%a == 0 {
		return a
	}
	return gcd(b%a, a)
}
0