Cutting Out of Factorials
InterviewTime limit1sMemory limit128 MB
Given k from 2 to 500, remove the fewest of 1!, 2!, ..., k! so the remaining product is a perfect square, and report that count.
- Level
Medium6 of 10
- Topics
- Math, Number theory, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
The factorial of a positive integer , written , is the product of all integers from to : .
Consider the product of the first factorials, . You may remove some of these factorials entirely from the product. The goal is to make the product of the remaining factorials a perfect square (the square of some integer).
Determine the minimum number of factorials that must be removed so that the product of the remaining factorials is a perfect square.
Input
A single line contains the integer ().
Output
Output a single line containing the minimum number of factorials that must be removed so that the product of the remaining factorials is a perfect square.