// // breakingSticks.swift // codesprint 12 // // Created by Avery Jones on 2017-12-14. // Copyright © 2017 Avery Jones. All rights reserved. // import Foundation // MARK: Funcs func factors(number : Int) -> [Int] { let wheel = [1,2,2,4,2,4,2,4,6,2,6] var n = number var returnArray = [Int]() var w = 0 var f = 2 while f*f <= number { while n % f == 0 { returnArray.append(f) n /= f } f = f + wheel[w] w += 1 if w == 11 { w = 3 } } if n > 1 { returnArray.append(n) } return returnArray } func biggestPrime(factors: [Int]) -> Int { return factors.last! } func breakBar(size : Int, numberOfPieces : Int, pieces :inout [Int]) -> [Int] { pieces[size] -= 1 let intoSize = size / numberOfPieces pieces[intoSize] += numberOfPieces return pieces } func breakBarToo(currentSize: Int, numberOfPieces: Int) -> Int { let intoSize = currentSize / numberOfPieces return intoSize } func eatPiece(pieces : inout [Int]) -> [Int] { pieces[1] -= 1 return pieces } func largestPiece(pieces : [Int], startingFrom: Int) -> Int { for a in stride(from: startingFrom, to: 0, by: -1) { if(pieces[a] != 0){ return a } } return 0 } func makeMove(pieces : inout [Int], biggestPiece : inout Int, totalPieces : inout Int) -> ([Int], Int, Int) { if biggestPiece == 1 && totalPieces > 0 { pieces = eatPiece(pieces: &pieces) totalPieces -= 1 } else { //let piecesAdded = smallestOdd(factors: factors(number: biggestPiece)) let piecesAdded = biggestPrime(factors: factors(number: biggestPiece)) pieces = breakBar(size: biggestPiece, numberOfPieces : piecesAdded, pieces: &pieces) if pieces[biggestPiece] == 0 { biggestPiece /= piecesAdded } totalPieces += piecesAdded - 1 } return (pieces, biggestPiece, totalPieces) } func makeMoveToo(currentSize : Int, currentCount: Int) ->(Int, Int, Int) { let piecesAdded = biggestPrime(factors: factors(number: currentSize)) return (breakBarToo(currentSize: currentSize, numberOfPieces:piecesAdded), piecesAdded*currentCount, currentCount) } // MARK: Main /* let n = Int(readLine()!)! let lengths = readLine()!.components(separatedBy: " ").map{ Int($0)! } var maxPiece = lengths.sorted().last! var biggestPiece = 0 var biggestCount = 1 var pieces = Array(repeating: 0, count: maxPiece + 1) var totalPieces = 1 var count = 0 for a in lengths { pieces[a] += 1 biggestPiece = a totalPieces = 1 while totalPieces > 0 { (pieces, biggestPiece, totalPieces) = makeMove(pieces: &pieces, biggestPiece: &biggestPiece, totalPieces: &totalPieces) count += 1 } } print(count) */ let n = Int(readLine()!)! let lengths = readLine()!.components(separatedBy: " ").map{ Int($0)! } var maxPiece = lengths.sorted().last! var biggestPiece = 0 var biggestCount = 1 var breakCount = 0 var totalPieces = 1 var count = 0 for a in lengths { biggestCount = 1 biggestPiece = a totalPieces = 1 breakCount = 0 while biggestPiece > 1 { (biggestPiece, biggestCount, breakCount) = makeMoveToo(currentSize: biggestPiece, currentCount: biggestCount) //print("Currently the biggest piece is of size \(biggestPiece) and there are \(biggestCount) of them") count += breakCount } count += biggestCount } print(count)