_OTZ_ @ 2024-10-19 09:22:30
module Main where
import Control.Monad.Reader
import Control.Monad.State
import Data.Char
main :: IO ()
main = do
n <- read <$> getLine
m <- reverse <$> getLine {- for high-precision calculations, start with single digits and work backwards -}
putStrLn . flip runReader n . flip evalStateT 0 $ judge 0 m
type CarryBaseEnv = StateT Int {- carry -} (Reader Int {- base -})
addBaseN :: Char -> Char -> CarryBaseEnv Char
addBaseN ch1 ch2 = do
baseN <- ask
carry <- get
let [num1, num2] = digitToInt <$> [ch1, ch2]
(carryNext, rest) = divMod (num1 + num2 + carry) baseN
put carryNext
return . intToDigit $ rest
addBaseN' :: String -> String -> CarryBaseEnv String
addBaseN' str1 str2 = do
put 0
eachBit <- foldl (\x y -> (:) <$> y <*> x) (return mempty) $ uncurry addBaseN <$> zip str1 str2
carry <- get
return . (if carry == 1 then ('1' :) else id) . reverse $ eachBit
judge :: Int -> String -> CarryBaseEnv String
judge n str = if n < 30
then
let rStr = reverse str
in if str == rStr then return . showString "STEP=" $ show n else addBaseN' str rStr >>= (judge (n + 1))
else return "Impossible!"
by _OTZ_ @ 2024-10-19 13:32:28
在 compiler explorer 上也使用 ghc 9.4.5 测试通过第一个示例,但是提交后是 RE,请大佬们帮忙看看
by Earth_Sky @ 2024-11-02 09:01:02
有没有可能是因为洛谷换行不是\n
by _OTZ_ @ 2024-11-02 15:00:19
@Earth_Sky 谢谢大佬, 我又在洛谷的在线ide上试了一下, 一直卡在提交中。 感觉永远执行不完, 但同样的示例在 compiler explorer 中可以, 感觉是洛谷的在线环境导致的问题。
by _OTZ_ @ 2024-11-02 15:14:02
又修改了一下,现在可以在洛谷在线ide执行了,但是提交还是不对,太奇怪了
module Main where
import Control.Monad.Reader
import Control.Monad.State
import Data.Char
main :: IO ()
main = do
n <- read <$> getLine :: IO Int
m <- reverse <$> getLine {- for high-precision calculations, start with single digits and work backwards -} :: IO String
putStr . flip runReader n . flip evalStateT 0 $ judge 0 m
type CarryBaseEnv = StateT Int {- carry -} (Reader Int {- base -})
addBaseN :: Char -> Char -> CarryBaseEnv Char
addBaseN ch1 ch2 = do
baseN <- ask
carry <- get
let [num1, num2] = digitToInt <$> [ch1, ch2]
(carryNext, rest) = divMod (num1 + num2 + carry) baseN
put carryNext
return . intToDigit $ rest
addBaseN' :: String -> String -> CarryBaseEnv String
addBaseN' str1 str2 = do
put 0
eachBit <- foldl (\x y -> (:) <$> y <*> x) (return mempty) $ uncurry addBaseN <$> zip str1 str2
carry <- get
return . (if carry == 1 then ('1' :) else id) . reverse $ eachBit
judge :: Int -> String -> CarryBaseEnv String
judge n str = if n >= 0 && n < 30
then
let rStr = reverse str
in if str == rStr then return . showString "STEP=" $ show n else addBaseN' str rStr >>= (judge (n + 1))
else return "Impossible!"
by _OTZ_ @ 2024-11-02 15:22:39
@Earth_Sky 谢谢大佬, 确实是这样, 当从下载下来的文件读入时, 会多出一个/r, 破案了
by Earth_Sky @ 2024-11-03 21:46:51
@OTZ 我不是学haskell的,这个几乎是很多人的难点