結果

問題 No.219 巨大数の概算
ユーザー tottoripaper
提出日時 2018-03-07 20:56:19
言語 Haskell
(9.10.1)
結果
AC  
実行時間 263 ms / 1,500 ms
コード長 983 bytes
コンパイル時間 3,396 ms
コンパイル使用メモリ 174,552 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-10-04 09:47:25
合計ジャッジ時間 18,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 51
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import           Control.Monad (replicateM)
import           Data.Fixed
import           Text.Printf

data E40 = E40
instance HasResolution E40 where
  resolution _ = 10 ^ 40

type F40 = Fixed E40

(#*) :: (F40, Integer) -> (F40, Integer) -> (F40, Integer)
(a, s) #* (b, t) = let c = a * b
                   in if c < 10.0 then (c, s + t) else (c / 10.0, s + t + 1)

expt :: (F40, Integer) -> Integer -> (F40, Integer)
expt = expt' (1.0, 0)
  where
    expt' b a 0 = b
    expt' b a n
      | n `mod` 2 == 1 = expt' (b #* a) (a #* a) (n `div` 2)
      | otherwise = expt' b (a #* a) (n `div` 2)

toPair :: F40 -> (F40, Integer)
toPair x
  | x < 10.0 = (x, 0)
  | otherwise = let (y, s) = toPair (x / 10)
                in (y, s + 1)

main = do
  n <- readLn
  replicateM n $ do
    [a, b] <- fmap words getLine
    let a' = toPair $ read a
        b' = read b
        (x, y) = expt a' b'
        x' = floor (x * 10) :: Integer
    printf "%d %d %d\n" (x' `div` 10) (x' `mod` 10) y
0