OJ: calculate the number of integers in 1 & lt; = n & lt; 1e9 that are multiples of 2, 3, and 5. oj1e9

Source: Internet
Author: User
OJ: The principle of exclusion and exclusion calculates the number of integers that are multiples of 2, 3, 5 in 1 <= n <1e9, oj1e9



I recently encountered a question in ACM, the meaning of the question is as follows,

 

Problem Description:

There is a sequence from 1 to n. Count the number that can be divided by 2, 3, 5. For example, when n = 6, there are 2, 3, 4, 5, and 6. These 5 numbers satisfy the condition, so we should output 5.
 

 

 

Enter

    Multiple sets of input to the end of the file, one for each group (n <1e9)
 

Output

    Output corresponding number
 

Sample input
1
     2
     6
 

Sample output
0
     1
     5
 

At first glance, it looks very simple. I wrote the following code without thinking about it.

 

#include <stdio.h>
long long main (void)
{
    long long n, i, flag = 0;
    while (scanf ("% lld", & n))
    {
      for (i = 1; i <= n; i ++)
        if (i% 2 == 0 || i% 3 == 0 || i% 5 == 0)
        flag ++;
        printf ("% lld \ n", flag);
        flag = 0;
    }
}
 

 

 

As soon as the submission time was exceeded, I debugged it on the local compiler. After reading the question (n <1e9), I entered 99999999. The result was not calculated for a long time. This must time out. The time limit for the question is 1000 ms Limit is strange.

Since this is not possible, we need to optimize the algorithm. Later, I saw this blog http://blog.csdn.net/u010885899/article/details/48022633 The content of the article is about using the principle of exclusion to calculate 1 <= N which is a multiple of 2, 3, 5, 7 The number of integers.

Inspired by this, Baidu encyclopedia contains the principle of exclusion:

计数 When counting, you must pay attention to no duplication and omissions. In order to prevent the overlapping part from being repeatedly calculated, people have developed a new counting method. The basic idea of this method is: first, regardless of the overlap, first calculate the number of all objects contained in a content, and The number of repeated calculations when counting is excluded, so that the calculation results are neither omitted nor repeated. This method of counting is called the principle of exclusion.

If there are three types of things to be counted, then the sum of the number of elements of types A, B and C = the number of elements of type A + the number of elements of type B + the number of elements of type C-both A It is also the number of elements of type B-both the number of elements of type A and C-the number of elements of both type B and C + the number of elements of both type A and B and type C. (A∪B∪C = A + B + C-A∩B-B∩C-C∩A + A∩B∩C)

From this definition, you can draw scoops according to the gourd.

的 Events divisible by 2 are recorded as class A, events divisible by 3 are recorded as class B, and events divisible by 5 are recorded as class C. The number of integers divisible by 2 3 5 can be expressed as follows: number of elements of type A + number of elements of type B + number of elements of type C-both the number of elements of type A and B-both type B and C Number of class elements — number of both class A and class B elements + number of class A and class B and class C elements (ie number that can be divided by 2 or 3 or 5 = number that can be divided by 2 Dividable number + number that can be divided by 3 + number that can be divided by 5-number that can be divided by 2 3 at the same time-number that can be divided by 2 5 at the same time-number that can be divided by 3 5 + Number that can be divided by 2 3 5 at the same time)

Then you can write the corresponding code

#include <stdio.h>
int main (void)
{
    int n, num;
    int a, b, c;
    int ab, ac, bc;
    int abc;
    while (scanf ("% d", & n))
    {
        a = n / 2;
        b = n / 3;
        c = n / 5;

        ab = n / (2 * 3);
        ac = n / (2 * 5);
        bc = n / (3 * 5);

        abc = n / (2 * 3 * 5);

        num = a + b + c-ab-ac-bc + abc;
        printf ("% d \ n", num);
    }
}
 

 

 

 Submit. Bingo is no problem. I also tested it locally. If the input is slightly larger than 1 e 9, the calculation result will be less than 1000 ms.
Related Article

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.