1284. reference time limit of 2 3 5 7: 1 second space limit: 65536 kb score: 5 gives a number N, evaluate 1 to n, the number is not a multiple of 2, 3, 5, and 7. For example, if n = 10, only 1 is not a multiple of 2, 3, 5, and 7. Input
Enter 1 N (1 <= n <= 10 ^ 18 ).
Output
The total number of outputs is not a multiple of 2, 3, 5, and 7.
Input Example 10 output Example 1 because N is very large, direct enumeration is not feasible, but the solution can be quickly obtained by using the exclusion theorem. It is the knowledge of high school mathematics. That is to say, sometimes it is difficult to calculate some things directly, so first calculate all, and then subtract the non-conforming things. To calculate the size of the union of several sets, we need
Single SetAnd then subtract all
Intersection of Two SetsAnd add all
Intersection of three setsAnd then subtract all
Intersection of Four SetsAnd so on.
Intersection of all sets. The result is N-(n/2 + N/3 + N/5 + N/7-n/6-n/10-n/14-n/15-n/21-n/35 + N/30 + N/ 42 + N/70 + N/105-n/210 )); reference blog: http://www.cppblog.com/vici/archive/2011/09/05/155103.html
#include<cstdio>int main(){ __int64 n; scanf("%I64d",&n); printf("%I64d",n-(n/2+n/3+n/5+n/7-n/6-n/10-n/14-n/15-n/21-n/35+n/30+n/42+n/70+n/105-n/210)); return 0;}
51-nod-1284 2 3 5 7 Multiple