Obtain the prime number by the screening method of the unique decomposition theorem of the ultraviolet A 10375 (number theory]

Source: Internet
Author: User

The basic content of the unique decomposition theory:

Any positive integer greater than 1 can represent the product of several prime numbers, and the representation method is unique. In other words, a number can be uniquely divided into the product of a prime factor. Therefore, this theorem is also called the unique decomposition theorem.

Example: 50 = (2 ^ 1) * (5 ^ 2)

The general idea of the question is to create a prime number table. E = {, 0, 0 ......}

The following are two questions. Let's just talk about the general idea:

Question 1:

E = (x1 * X3 * X4 *... * XK)/X2 judge whether E is an integer

If you set (x1 * X3 * X4 *... * XK) is divided into prime numbers, and X2 is also divided into prime numbers, if (x1 * X3 * X4 *... * XK) If X2 can be divisible, then the exponent of each prime number must be (x1 * X3 * X4 *... * XK) is larger than X2. If one is smaller than X2, it indicates that it is not an integer.

Question 2:

Known C (m, n) = M! /(N! (M-n !)), Input integer P, Q, R, S, (P> = Q, R> = s, pqrs <10000), calculate C (p, q)/C (r, s), the output must not exceed 10 ^ 8, and keep the five decimal places.

The first time I saw this question, I had no idea. After I learned the unique decomposition theory, I had some ideas,

Let's think about this question: we set C (p, q)/C (R, S) = (P! * S! * (R-s )!) /(Q! * (P-q )! * R !)

C (p, q)/C (R, S) = [p * (p-1)... (p-q + 1) * s! ]/[R * (r-1)... (R-S + 1) * Q! ]

However, it is not possible to multiply all the numbers and then divide the numbers into quality factors and the number of qualitative factors in the numerator increases, number of quality factors decomposed by denominator is reduced

Finally, check all the quality factors. If the number is positive and the number is negative, Division is required.

So how should we write it? Based on the above knowledge, we should easily understand our thoughts .......

The following garbage code is attached:

# Include <stdio. h> # include <iostream> # include <vector> # include <math. h >#include <cstring> using namespace STD; # define maxn 10005int E [maxn];/** e Represents the prime coefficient of each prime number {2, 3, 5, 7, 11 ......} * // ** if the VIS array is marked as 0, it indicates the prime number */INT vis [maxn]; void getprimevis (int n) {int M = SQRT (n + 0.5 ); memset (VIS, 0, sizeof (VIS); For (INT I = 2; I <= m; I ++) for (Int J = I * I; j <= N; j + = I) vis [J] = 1;}/** prime array: Prime [] = {2, 3, 5, 7, 11, 13 ......} */vector <int> prim E; void getprimeprime (int n) {for (INT I = 2; I <= N; I ++) if (vis [I] = 0) Prime. push_back (I);} // change n to a prime number and exist in the E array. Perform the addition and subtraction operation void add_integer (INT N, int D) on the E array) {for (INT I = 0; I <Prime. size (); I ++) {While (N % prime [I] = 0) N/= prime [I], E [I] + = D; if (n = 1) Break ;}// it indicates that the result is multiplied by (N !) ^ Dvoid add_factorial (int n, int d) {for (INT I = 1; I <= N; I ++) add_integer (I, d ); // The result is multiplied one by one, and finally N !} Int main () {int P, Q, R, S; getprimevis (maxn-1); getprimeprime (maxn-1); While (CIN> P> q> r> S) {memset (E, 0, sizeof (e); add_factorial (P, 1); add_factorial (Q,-1); add_factorial (p-Q,-1 ); add_factorial (R,-1); add_factorial (s, 1); add_factorial (r-s, 1); double ans = 1; for (INT I = 0; I <Prime. size (); I ++) ans * = POW (prime [I], E [I]); printf ("%. 5lf \ n ", ANS );}}


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.