Topic Five, 2 3 5 7multiples of
Given a number n, ask 1 to N, how many are not multiples of 2 3 5 7. For example, n = 10, only 1 is not a multiple of 2 3 5 7.
Input
Enter 1 number n (1 <= n <= 10^18).
Output
The output is not the number of multiples of 2 3 5 7.
Input example
10
10000
1234567890
98765
11111111111111111
Output example
1
2285
282186946
22575
2539682539682540
Problem Solving Ideas:
First say I see the idea of this problem, enumerate all integers 1 through N with an if judgment i%2| | i%3| | i%5| | I%7, if true, then the ans counter plus one, time complexity O (n),
In the case of 1 <= N <=10^18 directly GG Bar.
So I think that in the prime number of the Euler sieve in the table, you can imagine that the 1 to N all the numbers in 10 units, the second fourth fifth sixth eighth tenth number do not judge, only need to judge each group, The first element, the third element, the seventh element, and the nineth element are multiples of 3 and 7 . This time complexity is slightly lower than the first one, encountered the tree or directly GG .
The third method I changed a train of thought, the same remainder theorem, the specific theory Baidu is good. In the subject, the number in multiples of 1 to N is not 2 3 5 7
= number of multiples of 2 in N-n
-N is the number of multiples of 3
-N is the number of multiples of 5
-N is the number of multiples of 7
+n is the number of multiples of 2 and 3 .
+n is the number of multiples of 2 and 5 .
+n is the number of multiples of 2 and 7 .
+n is the number of multiples of 3 and 5 .
+n is the number of multiples of 3 and 7 .
+n is the number of multiples of 5 and 7 .
The number of multiples of 2,3,5 in -n
The number of multiples of 2,3,7 in -n
The number of multiples of 2,5,7 in -n
The number of multiples of 3,5,57 in -n
+n is the number of multiples of 2,3,5,7 ;
The time complexity drops from o (n) to O (1), the code is the addition and subtraction of several division operations to calculate the results, note that the n here to a long Long storage.
Code:
#include <cstdio>intMain () {Long LongN; Long LongCount =0; scanf ("%lld", &N); Count= N-n/2N3N5N7+ N/(2*3) + N/(2*5) + N/(2*7) + N/(3*5) + N/(3*7) + N/(5*7) -N/(2*3*5)-N/(2*5*7)-N/(2*3*7)-N/(3*5*7) + N/(2*3*5*7); printf ("%lld", Count); return 0;}
Multiples of 2 3 5 7