Multiples of H-2 3 5 7

Source: Internet
Author: User
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.
Sample Input

10

Sample Output

1

Contains the principle of repulsion, which is the formula.
The sum of the four circles is the total of four separate areas, minus four of the two-storey area where 22 intersect, plus the area of four 33-storey intersecting three, minus four intersecting areas. And the last one is. This question, too, is just a few more.

#include <stdio.h>
int main () {
    long num=0,n,a,b,c,d,ab,ac,ad,bc,bd,cd,abc,abd,acd,bcd,abcd;
    scanf ("%i64d", &n);

    A=N/2;
    B=N/3;
    C=N/5;
    D=N/7;

    AB=N/6;
    AC=N/10;
    AD=N/14;
    BC=N/15;
    BD=N/21;
    CD=N/35;

    ABC=N/30;
    ABD=N/42;
    ACD=N/70;
    bcd=n/105;

    abcd=n/210;

    NUM=A+B+C+D-AB-AC-AD-BC-BD-CD+ABC+ABD+ACD+BCD-ABCD;
    printf ("%i64d\n", N-num);
}

This problem has been done before, and then did not look at the other people's blog feel that they have done, but the fact is that they do not have any reminders, or will not do. Want to explain a question, do a question to do all write once, especially why, why did not think, because after looking at someone else's blog feel simple, that is to say that they should think out, but do not want to come out, they have to reflect on why, rather than feel like is separated by a layer of window paper, But this layer of window paper you don't know is bastion.
At that time we were feeling violence, but the fact is that, only with a for loop, so that, anyway, 10^18 must have timed out, so at the time also thought of should use binary or some kind of reduction in the calculation of the method, but it is not to do, but also think of multiples of the problem, even some people think of prime, But the fact is that the prime judgment condition is not as good as these 4 numbers, and this is not the case, 13*13, not prime but also not a multiple of 2 3 5 7, so the prime this line of thought is not right, and finally see someone else's code, suddenly remembered to contain the exclusion principle, so that you can directly perform the operation.
The code is very simple, it is not posted here, just leave a warning.

This time there is to see the amount of data can be violent, if possible, direct violence to solve. This is to investigate whether you will make an estimate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.