Multiples of 2 3 5 7

Source: Internet
Author: User

Base time limit: 1 seconds space limit: 131072 KB Score: 5 Difficulty: 1-level algorithm problem

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
Output example
1

Said ashamed, thought of a night to finally figured out the principle of repulsion ... But fortunately finally figured out!

As the subject of the Four Yuan 2 3 5 7, by the test instructions can get {N/2}{N/3}{N/5}{N/7} four sets, then we can use the theorem to calculate the size of the four set merge set Num,n-num that is the request.

The calculation method of the combined set uses the tolerant theorem, that is, the total number of four sets minus 22 intersection number plus 33 The number of intersections and then minus 44 intersection number ....

The AC code is attached:

1#include <iostream>2 using namespacestd;3 4 intMain () {5     Long LongN;6Cin>>N;7     Long LongA,B,C,D,AB,AC,AD,BC,BD,CD,ABC,ABD,ACD,BCD,ABCD;8     Long Longnum=0;9a=n/2;Tenb=n/3; Onec=n/5; Ad=n/7; -      -ab=n/6; theac=n/Ten; -ad=n/ -; -bc=n/ the; -bd=n/ +; +cd=n/ *; -      +abc=n/ -; Aabd=n/ the; atbcd=n/ the; -acd=n/ -; -      -abcd=n/ About; -      -num=a+b+c+d-ab-ac-ad-bc-bd-cd+abc+abd+bcd+acd-ABCD; incout<<n-num<<Endl; -     return 0; to}

Multiples of 2 3 5 7

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.