51nod 1284:2 3 5 7 multiple-repulsion principle

Source: Internet
Author: User

1284 2 3 5 7 multiplier base time limit: 1 seconds space limit: 131072 KB Score: 5 Difficulty: 1-level algorithm topic collection focus on giving a number n, 1 to N, how many of the number is not a multiple 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
the principle of tolerance is described as follows:

To calculate the size of several sets of merge sets, we first calculate the size of a single collection, subtract the part that intersects the two sets, add the portion that intersects the three sets, subtract the portion of four that intersect, and so on, all the parts that intersect the collection.

So the question is not a multiple of 2 3 5 7, which can be expected after a multiple of 2 3 5 7, then minus.

This will use the principle of repulsion, first of all, just divide the number of 2 3 5 7, minus the total number of the product of 22 between them, plus the number of products that divide them by three, plus the number divisible by four.

This number is also interesting, the number of 1 to num divisible by C is the value of num/c.

So, the code:

#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string > #include <cstring> #pragma warning (disable:4996) using namespace Std;long long n,num,a,b,c,d,ab,ac,ad,bc,bd, Cd,abc,abd,bcd,acd,abcd;int Main () {CIN>>N;NUM=0;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;cout<<n-num<<endl;return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

51nod 1284:2 3 5 7 multiple-repulsion principle

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.