Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:04:05+00:00 2026-05-23T12:04:05+00:00

I am trying to solve this problem in Haskell but getting time limit exceed.

  • 0

I am trying to solve this problem in Haskell but getting time limit exceed. I applied all my Haskell and mathematical skill to optimize this but all in vain. Could some one please suggest me how to optimize this code further. The sequence F_3 + F_7 + F_11 …. + F_(4n+3) = F_2n*F_(2n+1). I used O(log n) to method to calculate the Fibonacci numbers.

import Data.List
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as BS

matmul :: [Integer] -> [Integer] -> Integer -> [Integer]
matmul [a,b,c] [d,e,f] m = [x,y,z] where
    y = (a*e + b*f) `mod` m
    z = (b*e + c*f) `mod` m
    x = y + z


powM ::[Integer] -> Integer -> Integer -> [Integer]
powM a n m | n == 1 = a 
   | n == 2 = matmul a a m
   | even n = powM ( matmul a a m ) ( div n 2 ) m 
   | otherwise = matmul a ( powM ( matmul a a m ) ( div n 2 ) m ) m 

readInt :: BS.ByteString -> Integer
readInt  = fst.fromJust.BS.readInteger 

solve::Integer -> BS.ByteString
solve n = BS.pack.show $ mod ( c*d ) 1000000007 where 
 [c,d,_] = powM [1,1,0] ( 2*n ) 1000000007
--([_,a,_]:_) = powM [[1,2,1],[0,5,3],[0,3,2]] n 1000000007
-- f_3+f_7+f_11+f_15 = f_2n*f_(2n+1)

main = BS.interact $ BS.unlines. map ( solve.readInt ) . tail . BS.lines 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:04:06+00:00Added an answer on May 23, 2026 at 12:04 pm

    Your solving seems to be fast enough but it seems that your main function does not print the answer after each new line. In fact it requires an extra newline to get the last answer so this can be the cause of your timeout! Here is a version that prints each answer directly after the input.

    import Data.List
    import Data.Maybe
    import Control.Monad
    import qualified Data.ByteString.Lazy.Char8 as B
    import qualified Data.ByteString.Char8 as BC
    import qualified Text.Show.ByteString as BS
    
    matmul :: [Integer] -> [Integer] -> Integer -> [Integer]
    matmul [a,b,c] [d,e,f] m = [x,y,z] where
        y = (a*e + b*f) `mod` m
        z = (b*e + c*f) `mod` m
        x = y + z
    
    powM :: [Integer] -> Integer -> Integer -> [Integer]
    powM a n m | n == 1 = a 
       | n == 2 = matmul a a m
       | even n = powM ( matmul a a m ) ( div n 2 ) m 
       | otherwise = matmul a ( powM ( matmul a a m ) ( div n 2 ) m ) m 
    
    solve :: Integer -> Integer
    solve n = mod ( c*d ) 1000000007 
      where 
      [c,d,_] = powM [1,1,0] ( 2*n ) 1000000007
    
    readInteger :: B.ByteString -> Integer
    readInteger  = fst . fromJust . B.readInteger
    
    readInt :: B.ByteString -> Int
    readInt = fst . fromJust . B.readInt
    
    get :: IO B.ByteString
    get = liftM (B.fromChunks . (:[])) BC.getLine
    
    main :: IO ()
    main = do
      n <- liftM readInt get
      replicateM_ n ( liftM readInteger get >>= B.putStrLn . BS.show . solve )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve this problem all afternoon but with no luck, hopefully someone
I've been trying to solve this problem for a number of days now but
I have been trying to solve this problem for a while, but couldn't with
I've been trying to solve this problem for a couple of hours but I
I've been trying to solve this problem for quite sometime but I am having
I don't understand anything about IIS, but am trying to solve this problem of
I have this problem and I'm trying to solve it using jsfiddle but I'm
I am trying to solve this problem: In an integer array all numbers occur
I am very interested in Haskell and currently trying to solve this problem. To
I have been trying to solve this problem all day, I googled a lot,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.