An in-depth analysis of the implementation method of C-language decomposition decomposition _c language

Source: Internet
Author: User

First of all, let's look at one of the simplest C-language implementations of mass factorization:

#include <stdio.h>
void Main ()
{
  int data, i = 2;
  scanf ("%d", &data);
  while (Data > 1)
  {
    if (data% i = = 0)
    {
      printf ("%d", I);
      Data/= i;
    }
    else i++;
  }
}

Principle && Method
to decompose a number into the form of a product of several decomposition, the process of finding a mass factor is called the decomposition decomposition, and the factorization is only for composite numbers.

To find a number of decomposition decomposition, from the smallest prime numbers, always except until the result is prime. The decomposition of the decomposition formula is called Short division, and division is similar to the nature of the polynomials can also be used to find multiple numbers of:

Take 24 as an example:

2--24

2--12

2--6

3 (3 is prime number, end)

Draw = 2x2x2x3 = 2^3 * 3


Code
Can be used to filter the number of prime numbers to meet the conditions of the quality factor, and then for loop traversal, through a topic to show this part of the code

Topic 1

Topic Description:
The number of the mass factor of the positive integer N (n>1) is obtained.
The same mass factor needs to be calculated repeatedly. such as 120=2*2*2*3*5, a total of 5 qualitative factors.
Input:
There may be multiple sets of test data, and the input for each set of test data is a positive integer N, (1<n<10^9).
Output:
For each set of data, the number of mass factors for the output n.
Sample input:
120
Sample output:
5
Tips:
Note: 1 is not a mass factor of N, if n is prime, n is the mass factor of N.


AC Code

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

Deep understanding
My so-called deep understanding is to use the 4 star problem to flexibly apply the decomposition Decomposition method, the topic is as follows

Topic 2

Topic Description:
Given the N,a Max K, so that the n! Can be divisible by a^k but not divisible by a^ (k+1).
Input:
two integers n (2<=n<=1000), A (2<=a<=1000)
Output:
An integer.
Sample input:
6 10
Sample output:
1

Ideas
A^k and n! can be very large, or even more than the range of long int, so there is no direct use of the residual operation to determine whether there is an integer relationship between them, so we need to change the idea, starting with the decomposition decomposition, assuming two numbers a and B:

A = p1^e1 * p2^e2 * ... * pn^en, B = p1^d1 * P2^D2 * ... * pn^dn

, b divided by a can be expressed as:

b/a = (P1^D1 * p2^d2 * ... * pn^dn)/(P1^E1 * p2^e2 * ... * pn^en)

If B can be divided by a, then b/a must be integer, and two primes must be a quality, we can draw the following law:

If a exists decomposition px, then B must also exist the decomposition, and the power exponent of the corresponding factor in B must not be less than the power exponent in a


Another B = n!, a^k = p1^ke1 * P2^ke2 * ... * pn^ken, so we need to determine the largest nonnegative integer k. For this k, we only need to test each element of a in turn, and determine how many times the factor in B is the power exponent of the factor in a, and the smallest of all multiples is the k we are seeking.

Analysis here, the rest of the work seems to be only a and n! decomposition decomposition, but the n! calculated and then decomposed decomposition, so n! value is too large. Considering the number of element p in n!, the power exponent corresponding to the factor p is determined. We know that n! contains the product of all integers from 1 to n intervals, the multiples of each p of these products (including itself) contribute at least one P factor to n!, and we know that the multiples of p in 1 to n are in total n/p. Similarly, computing p^2,p^3,... Can

Code

#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 1001 int pri 
    
  Me[n], size; 
      
    /** * Prime number Screening method for preprocessing/void initprocess () {int i, J; 
    for (prime[0] = prime[1] = 0, i = 2; i < N; i + +) {Prime[i] = 1; 
    
    size = 0; 
        for (i = 2; i < N; i + +) {if (Prime[i]) {size + +; 
        for (j = 2 * i; J < N; J + = i) {Prime[j] = 0; 
      
    int main (void) {int i, n, a, K, NUM, Count, base, TMP, *ansbase, *ansnum; 
    
    Pretreatment initprocess (); 
      while (scanf ("%d%d", &n, &a)!= EOF) {ansbase = (int *) calloc (size, sizeof (int)); 
    
      Ansnum = (int *) calloc (size, sizeof (int)); 
          Decompose a decomposition for (i = 2, num = 0; i < N && a!= 1; i + +) {if (Prime[i] && a% = = 0) { 
          Ansbase[num] = i; 
        Ansnum[num] = 0;    
          while (a!= 1 && a% i = = 0) {Ansnum[num] + = 1; 
          A = a/i; 
        num + +; 
        }///min K for (i = 0, k = 0x7fffffff i < num i + +) {base = Ansbase[i]; 
        Count = 0; 
          while (base <= N) {count = N/base; 
        Base *= Ansbase[i]; 
        } TMP = Count/ansnum[i]; 
      if (TMP < k) k = tmp;  
    printf ("%d\n", K); 
  return 0; }/************************************************************** problem:1104 User:wangzhengyi L Anguage:c result:accepted time:0 Ms memory:916 KB **************************************************** 
 ************/

Theorem of approximate number of numbers
for a positive integer greater than 1, n can decompose decomposition:

n = p1^a1 * P2^A2 * p3^a3 * ... * pn^an

, the number of positive divisors for n is:

 (A1 + 1) * (A2 + 1) * ... * (an + 1)

. of which P1,P2,.. PN is the mass factor of N, A1, a2...an is P1,p2,.. The index of the PN

Prove
n can be decomposed decomposition: N=P1^A1 * p2^a2 * p3^a3 * ... * pk^ak,

By the definition of divisors, it is known that the divisors of P1^A1 are: p1^0, p1^1, P1^2......P1^A1, Total (a1+1); the approximate divisors of P2^A2 are (a2+1). There are (Pk^ak) divisors of ak+1.

So according to the multiplication principle: n The number of divisors is

(a1+1) * (a2+1) * (a3+1) *...* (ak+1)

Topic 3

Topic Description:
Enter n integers, sequentially outputting the number of divisors per number
Input:
Input first behavior N, the number of arrays (n<=1000)
The next 1 lines include n integers, each of which has a range of (1<=num<=1000000000)
Enter end when n=0.
Output:
There may be multiple sets of input data, for each group of input data,
Output n rows, where each row corresponds to the number of divisors of a number above.
Sample input:
5
1 3 4 6 12
Sample output:
1
2
3
4
6


Code

#include <stdio.h> #include <stdlib.h> #define N 40000 typedef long INT lint; 
    
  int prime[n], size; 
    
    void init () {int i, J; 
    for (prime[0] = prime[1] = 0, i = 2; i < N; i + +) {Prime[i] = 1; 
    
    size = 0; 
        for (i = 2; i < N; i + +) {if (Prime[i]) {size + +; 
      for (j = 2 * i; J < N; J + = i) prime[j] = 0; 
    }} lint Numprime (int n) {int i, num, *ansnum, *ansprime; 
    
    Lint count; 
    Ansnum = (int *) malloc (sizeof (int) * (size + 1)); 
    
    Ansprime = (int *) malloc (sizeof (int) * (size + 1)); for (i = 2, num = 0; i < n && n!= 1; i + +) {if (Prime[i] && N i = = 0) {Ansprime[nu 
        M] = i; 
        Ansnum[num] = 0; 
          while (n!= 1 && n% i = = 0) {Ansnum[num] + = 1; 
        n/= i; 
      num + +; } if (n!= 1) {Ansprime[num] = n; 
      Ansnum[num] = 1; 
    num + +; 
    for (i = 0, count = 1; i < num i + +) {count *= (Ansnum[i] + 1); 
    Free (ansnum); 
    
    Free (ansprime); 
  return count; 
    int main (void) {int i, n, *arr; 
    
    Lint count; 
    
    Init (); 
      while (scanf ("%d", &n)!= EOF && n!= 0) {arr = (int *) malloc (sizeof (int) * n); 
      for (i = 0; i < n; i + +) {scanf ("%d", arr + i); 
        for (i = 0; i < n; i + +) {count = Numprime (Arr[i]); 
      printf ("%lld\n", count); 
    Free (arr); 
  return 0; }/************************************************************** problem:1087 User:wangzhengyi Langua Ge:c result:accepted time:190 Ms memory:1068 KB ****************************************************** 
 **********/

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.