Problem:
Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

If there are multiple solutions, any of them will be accepted.

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

--{-# LANGUAGE OverloadedStrings #-}
--{-# LANGUAGE OverloadedLists   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns      #-} --引数に!
{-# LANGUAGE MultiWayIf        #-}
{-# LANGUAGE TupleSections     #-}

import Data.Bits
import Data.Foldable (foldlM,foldrM)
import Data.Maybe (fromJust)
import Data.List
import Data.Array  --変数衝突
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
import qualified Data.Vector.Unboxed as V
import Control.Monad
import Control.Monad.State
import Control.Monad.ST
import qualified Data.ByteString.Lazy.Char8 as B
import Debug.Trace

type Ans = String

-- h <= n <= w として
--4*h <= 3*N
--4*w >= 3*N

solve :: Int -> Ans
solve n = let
  infs = [max 1 (n`div`4)..3*(n+1)`div`4]
  sups = [3*n`div`4 .. 3500]
  ((h',n',w'):ans) = traceShowId {-find (\xs -> xs /= Nothing)-} [(h,h*w*n `div` (4*h*w-n*w-n*h),w) | h <- infs, w <- sups, 4*h*w-n*w-n*h /= 0, h*w*n `div` (4*h*w-n*w-n*h) > 0 ,h*w*n == (h*w*n `div` (4*h*w-n*w-n*h))*(4*h*w-n*w-n*h)]
  in (show h') ++ " " ++ (show n') ++ " " ++ (show w')

main :: IO ()
main = putStrLn . main0 =<< B.getContents

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

main0 :: B.ByteString -> Ans
main0 cont =
   let remLines0 = map B.words (B.lines cont)
       [n']:remLines1 = remLines0
       n = readBInt n'
   in solve n