Efficiently calculate the number of approx.

Source: Internet
Author: User
There are many ways to efficiently calculate the number of approx. You can use O (N)To check whether it is an approximate number. You can also use the Complexity Algorithm from 1 ~ To determine the number, but here we provide a slightly faster method, saving about 2/3 of the time.

Description:
Input n integers and output the number of approx.
Input:
The first input behavior n, that is, the number of arrays (n <= 1000)
The next line contains N integers. The range of each number is (1 <= num <= 1000000000)
When n = 0, the input ends.
Output:
Multiple groups of input data may exist. For each group of input data,
Output n rows, where each row corresponds to the number of an appointment in the preceding number.
Sample input:
5
1 3 4 6 12
Sample output:
1
2
3
4
6
Source:
Questions about the vitality of computer research at Tsinghua University in 2011

Solution:We need to use the Approximate Number Theorem for this question. Before that, we need to understand the following facts,

Theorem 1Given a number, there is only one prime number in the right integer.

Proof: Assume that there are two prime numbers on the right, which is in conflict with the question stem. Therefore, there is only one prime number at most.

Theorem 2(Approx. Number Theorem) a number can be expressed as the product of its prime power. That is, the number of approx. Is.


For this question, we first find all prime numbers smaller than or equal to, then obtain the corresponding exponent, and then use the Approximate Number Theorem to obtain the number of the approximate number. It is not complete yet. There may be a prime number on the right side. Can we determine whether there is a prime number on the right side? If yes, the total number of appointments is

This is because the prime number can be multiplied by any one on the left to form an approximate number, so the approximate number doubles. If not, the approximate number is.


Source code:

#include<stdio.h>void main(){    int N,n,i,s,r;    while(scanf("%d",&N) != EOF){        while(N--){            scanf("%d",&n);            s = 1;            for (i = 2; i * i <= n; i++) {                r = 0;                while (n % i == 0) {                    r++;                    n /= i;                }                if (r > 0) {                    r++;                    s *= r;                }            }            if (n > 1)                s *= 2;            printf("%d\n", s);        }    }}


Efficiently calculate the number of approx.

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.