Problem 552
Count Square-Free Numbers
Hard · 30 pts · Math, Number Theory, Inclusion-Exclusion
Problem
A positive integer is square-free if no perfect square greater than 1 divides it, so 10 = 2 × 5 is square-free but 12 = 4 × 3 is not. Count the square-free numbers from 1 to N. By inclusion-exclusion over squares, the count is the sum of μ(d) × floor(N / d²) for d = 1 .. floor(√N), where μ is the Möbius function: 0 if d has a squared prime factor, otherwise (-1) to the number of prime factors of d. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
A single integer N.
Output
Print the number of square-free integers from 1 to N.
Constraints
1 <= N <= 10^12
Example input
10
Example output
7
Explanation
From 1 to 10, the numbers 4, 8 and 9 are divisible by a square (4 or 9), and the other seven are square-free: 1, 2, 3, 5, 6, 7 and 10. So the answer is 7.