Basic algorithm Sưu tầm các thuật toán trong lập trình

87 1.3K 0
Basic algorithm    Sưu tầm các thuật toán trong lập trình

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

MỤC LỤC Algebra (23) basic algorithms (20) • Euler function and its calculation TeX • Binary exponentiation of O (log N) TeX • Euclids algorithm finding the GCD (greatest common divisor) TeX • Sieve of Eratosthenes TeX • Advanced Euclidean algorithm TeX • Fibonacci numbers and their rapid calculation TeX • The inverse of the ring modulo TeX • Gray code TeX • Long arithmetic TeX • Discrete logarithm modulo M algorithm babystepgiantstep Shanks for O (sqrt (M) log M) TeX • Diophantine equations with two unknowns: AX + BY = C TeX • Modular linear equation of the first order: AX = B TeX • Chinese remainder theorem. Garners algorithm TeX • Finding degree divider factorial TeX • Balanced ternary notation TeX • The factorial N modulo P for O (P log N) TeX • Enumeration of all subpatterns this mask. Grade 3 N to the total number of all subpatterns masks TeX • A primitive root. Algorithm for finding TeX • Discrete root extract TeX • Sieve of Eratosthenes with linear time work TeX complex algorithms (3) • Test BPSW the simplicity of numbers in O (log N) • Efficient algorithms for factorization Pollard p1, Pollard p, Bent, Pollard Monte Carlo Farm • Fast Fourier transform of O (N log N). Application to the multiplication of two polynomials or long numbers TeX Euler function Determination Euler function (sometimes labeled or ) the number of numbers to prime to . In other words, the number of segment numbers , the greatest common divisor with which is unity. The first few values of this function ( A000010 in OEIS encyclopedia ): Properties The following three simple properties of the Euler enough to know how to calculate it for any numbers: • If a prime number, then . (This is obvious, since any number except the prime to it.) • If simple a natural number, then . (As with the number of not only relatively prime numbers of the form , which pieces.) • If and are relatively prime, then (multiplicative Euler function). (This follows from the Chinese remainder theorem . Consider an arbitrary number . We denote by and the remnants of the division at and , respectively. Then prime to if and only if is prime to and separately, or what is the same, mutually simply and relatively prime to . Using the Chinese remainder theorem, we see that any pair of numbers and the number of onetoone correspondence , which completes the proof.) From here you can get the Euler function for all through his factorization (decomposition into prime factors): if (Where everything simple), the Implementation The simplest code calculating the Euler function, the quotient of the number of elementary method for : int phi (int n) { int result = n; for (int i=2; ii 1) result = result n; return result; } A key place for the calculation of the Euler function is to find the factorization of . It can be done in a time much smaller : see. Efficient algorithms for factorization . The application of Eulers function The most famous and important property of Euler expressed in Eulers theorem : where and are relatively prime. In the particular case when a simple Eulers theorem turns into the socalled Fermats little theorem : Eulers theorem occurs quite often in practical applications, for example, see. Reverse element in the modulus . Tasks in the online judges A list of tasks that require a function to calculate the Euler or use Eulers theorem, either by value of the Euler function to restore the original number: • UVA 10179 Irreducible Basic Fractions Difficulty: Low • UVA 10299 Relatives Difficulty: Low • UVA 11327 Enumerating Rational Numbers Difficulty: Medium • TIMUS 1673 Admission to the exam Difficulty: High Binary exponentiation Binary (binary) exponentiation is a technique that allows you to build any number of th degree of multiplications (instead of multiplications in the usual approach). Moreover, the technique described here is applicable to any of the associative operation, and not only to the multiplication numbers. Recall operation is called associative if for any executed: The most obvious generalization on balances of some module (obviously, associativity is maintained). The next popularity is a generalization to the matrix product (it is well known associativity). Algorithm Note that for any number and an even number of feasible obvious identity (which follows from the associativity of multiplication): It is the main method in binary exponentiation. Indeed, for an even we showed how, by spending only one multiplication, we can reduce the problem to a half less. It remains to understand what to do, if the degree is odd . Here we do is very simple: move on to the extent that would have an even: So, we actually found the recurrence formula: on the degree we go, if it chёtna to , and otherwise to . It is clear that there will be no more transitions, before we come to (the basis of the recurrence formula). Thus, we have an algorithm that works for multiplications. Implementation A simple recursive implementation: int binpow (int a, int n) { if (n == 0) return 1; if (n % 2 == 1) return binpow (a, n1) a; else { int b = binpow (a, n2); return b b; } } Nonrecursive implementation also optimized (divideby2 replaced with bit operations): int binpow (int a, int n) { int res = 1; while (n) if (n 1) { res = a; n; } else { a = a; n >>= 1; } return res; } This implementation can be more simplified somewhat by noting that the construction of the square is always performed, regardless of whether the condition is odd worked or not: int binpow (int a, int n) { int res = 1; while (n) { if (n 1) res = a; a = a; n >>= 1; } return res; } Finally, it is worth noting that the binary exponentiation is already implemented in the language of Java, but only for long arithmetic class BigInteger (pow function of this class is working on the construction of the binary algorithm). Examples of solving problems Efficient computation of Fibonacci numbers Condition . Given the number . You want to calculate where Fibonacci sequence . Solution . More details are described in the decision paper on the Fibonacci sequence . Here we only briefly we present the essence of the decision. The basic idea is as follows. The calculation of the next Fibonacci number is based on the knowledge of the previous two Fibonacci numbers: namely, each successive Fibonacci number is the sum of the previous two. This means that we can construct a matrix that will fit this transformation: how two Fibonacci numbers and calculate the next number, ie go to the pair , . For example, applying this transformation to a couple of times and we get a couple and .Thus, elevating the matrix of this transformation in the th degree, we thus find the required time for what we required. Erection reshuffle of th degree Condition . Given permutation of length . Required to build it in s degree, ie find out, if to the identity permutation permutation times apply . Solution . Simply apply to the interchange of the algorithm described above binary exponentiation. No differences as compared with the construction numbers in degree no. With the asymptotic solution is obtained . (Note. This problem can be solved more efficiently in linear time . To do this, simply select all the cycles in the permutation, then consider separately each cycle and taking the modulo length of the current cycle, to find an answer for this cycle.) Rapid application of a set of geometric operations to points Condition . Given points and are transformations that should be applied to each of these points. Each transformation a shift to a predetermined vector, or scaling (multiplication coefficients to specified coordinates), or around a predetermined rotation axis by a predetermined angle. Furthermore, there is a component of a cyclic repetition operation: it has the form a predetermined number of times to repeat a predetermined list of transformations (cyclic repetition of operations can be nested in each other). You want to calculate the result of applying these operations to all points (effectively, ie in less than than where the total number of transactions that need to be done). Solution . Look at the different types of transformations in terms of how they change the coordinates: • Shift operation it just adds to all the coordinates of the unit, the multiplication by constants. • Scaling operation it multiplies each coordinate by some constant. • Operation rotational axis it can be represented as follows: new coordinates obtained can be written as a linear combination of the old ones. (We will not specify how this is done. For example, for simplicity, imagine it as a combination of fivedimensional rotations: first, in the planes , and so that the axis of rotation coincides with the positive direction of the axis , then the desired rotation around an axis in the plane , then reverse rotation in the plane and so that the axis of rotation back to its starting position.) Easy to see that each of these transformations a recalculation of coordinates on linear equations. Thus, any such transformation can be written in matrix form : which when multiplied by (left) to the line of the old coordinates and the constant unit gives a string of new coordinates and constants units: (Why need to enter a dummy fourth coordinate is always equal to one? Without this we would have to implement the shift operation: after the shift this is just adding to the coordinates of the unit, the multiplication by some factors. Without the dummy unit we could only implement linear combinations of the coordinates themselves, and add to them given constants could not.) Now the solution of the problem becomes almost trivial. Once each elementary operation is described by the matrix, then the process described by the product of these matrices, and the operation of the cyclic repetition the erection of this matrix to a power. Thus, during the time we can predposchitat matrix describing all the changes, and then simply multiply each point on the matrix thus, we will respond to all requests for time . The number of paths in a fixed length column Condition . Given an undirected graph with vertices, and are given a number . Is required for each pair of vertices and the number of ways to find among them with exactly edges. Solution . More details on this problem is considered in a separate article . Here we only recall the essence of the decision: we simply erect a th degree of adjacency matrix of the graph, and the elements of this matrix and will be a solution. The resulting asymptotic behavior . (Note. In the same article, consider other variation of this problem: when the graph is weighted, and you want to find the path of minimum weight, containing exactly edges. As shown in this paper, this problem is also solved by means of binary exponentiation of the adjacency matrix of the graph, but instead of the usual operation of multiplication of two matrices to be used modified: instead of multiplying the amount taken, and instead of summation taking a minimum.) Variation binary exponentiation: multiplication of two integers modulo We give here an interesting variation of the binary exponentiation. Suppose we are faced with such a challenge : to multiply two numbers and modulo : Suppose that the numbers can be quite large: so that the numbers themselves are placed in a builtin data types, but their immediate work no longer exists (note that we also require that the sum of the numbers placed in a builtin data type). Accordingly, the task is to find the unknown quantity , without the help of a long arithmetic . The decision is as follows. We simply apply the algorithm binary exponentiation described above, but instead of multiplication, we will make the addition. In other words, multiplication of two numbers, we have reduced to the operations of addition and multiplication by two (which is also, in fact, there is the addition). (Note. This problem can be solved in another way , by resorting to the help of operations with floatingpoint numbers. Namely, the count in the floating point expression , and round it to the nearest integer. Thus we find the approximate quotient. take it away from the product (ignoring the overflow), we are likely to obtain a relatively small number that can be taken modulo and return it as an answer. This solution looks pretty unreliable, but it is very fast, and very briefly implemented.) Tasks in the online judges List of tasks that can be solved using a binary exponentiation: • SGU 265 Wizards Difficulty: Medium Euclids algorithm finding the GCD (greatest common divisor) Given two nonnegative integers and . Required to find their greatest common divisor, ie the largest number that divides both a and . English greatest common divisor is spelled greatest common divisor, and its common designation is : (Here the symbol denotes the divisibility, ie, means divide ) When it is of the numbers is zero, and the other is nonzero, their greatest common divisor, by definition, be it the second number. When both numbers are equal to zero, the result is not defined (any suitable infinite number), we put in this case the greatest common divisor of zero. Therefore, we can speak of such a rule: if one of the numbers zero, the greatest common divisor equal to the second number. Euclids algorithm , discussed below, solves the problem of finding the greatest common divisor of two integers and over . This algorithm was first described in the book of Euclids Elements (about 300 BC), although it is quite possible, this algorithm has an earlier origin. Algorithm The algorithm itself is extremely simple and is described by the following formula: Implementation int gcd (int a, int b) { if (b == 0) return a; else return gcd (b, a % b); } Using the ternary conditional operator C ++, the algorithm can be written even shorter: int gcd (int a, int b) { return b ? gcd (b, a % b) : a; } Finally, we present and nonrecursive algorithm form: int gcd (int a, int b) { while (b) { a %= b; swap (a, b); } return a; } Proof of correctness First, we note that for each iteration of the algorithm of Euclid its second argument is strictly decreasing, therefore, since it is nonnegative, then the Euclidean algorithmalways terminates . To prove the correctness we need to show that for any . We show that the quantity on the lefthand side is divided by this in the right, and the righthand is divided into the lefthand. Obviously, this will mean that the left and right sides of the same, and that proves the correctness of Euclids algorithm. Denote . Then, by definition, and . Next, we expand the remainder of the division on through their private: But then it follows: So, recalling the statement , we obtain the system: Now we use the following simple fact: if for any three numbers fulfilled: and then executed and: . In our situation, we get: Or, substituting its definition as we obtain: So, we spent half the evidence is shown that divides the left side of the right. The second half of the proof is similar. Operation time Time of the algorithm is evaluated Lame theorem , which establishes a surprising connection of the Euclidean algorithm and the Fibonacci sequence: If and for some , the Euclidean algorithm performs no more recursive calls. Moreover, it can be shown that the upper bound of the theorem optimal. When it will be done recursive call. In other words, successive Fibonacci numbers the worst input to the Euclidean algorithm. Given that the Fibonacci numbers grow exponentially (as a constant in degree ), we find that the Euclidean algorithm is performed for multiplication. LCM (least common multiple) The calculation of the least common multiple (least common multiplier, lcm) is reduced to the calculation of the following simple statement: Thus, the calculation of the NOC can also be done using the Euclidean algorithm, with the same asymptotic behavior: int lcm (int a, int b) { return a gcd (a, b) b; } (Here divided into profitable first , and only then is multiplied by , as this will help to avoid overflows in some cases) Literature • Thomas feed, Charles Leiserson, Ronald Rivest, Clifford Stein. Introduction to Algorithms 2005 Sieve of Eratosthenes Sieve of Eratosthenes an algorithm for finding all prime numbers in the interval of operations. The idea is simple we will write a series of numbers , and we will strike out at first all numbers divisible by , except for the number and then dividing by , except for the numbers , then on , then , and everything else is just up . Implementation Immediately we present the implementation of the algorithm: int n; vector prime (n+1, true); prime0 = prime1 = false; for (int i=2; i=1; i) vals powmod (a, i n, m) = i; for (int i=0; i maxx) shift_solution (x, y, a, b, sign_b); int rx1 = x; shift_solution (x, y, a, b, (miny y) a); if (y < miny) shift_solution (x, y, a, b, sign_a); if (y > maxy) return 0; int lx2 = x; shift_solution (x, y, a, b, (maxy y) a); if (y > maxy) shift_solution (x, y, a, b, sign_a); int rx2 = x; if (lx2 > rx2) swap (lx2, rx2); int lx = max (lx1, lx2); int rx = min (rx1, rx2); return (rx lx) abs(b) + 1; } Also, it is easy to add to this the conclusions of all the solutions: it is enough to sort out in the interval with a step , finding for each of them corresponding directly from the equation . Finding solutions in a given interval with the least amount of x + y Here at and should also be imposed any restriction, otherwise the answer will almost always negative infinity. The idea of the solution is the same as in the previous paragraph, first find any solution of the Diophantine equation, and then using the described procedure in the previous paragraph, we arrive at the best solution. Indeed, we have the right to perform the following transformation (see. The previous paragraph) Note that when this amount changes as follows: Ie if it is necessary to choose the smallest possible value , if it is necessary to choose the largest possible value . If we can not improve the solution all decisions will have the same amount. Tasks in the online judges List of tasks that can be taken on the subject of Diophantine equations with two unknowns: • SGU 106 The Equation Difficulty: Medium Modular linear equation of the first order Statement of the Problem This equation is of the form: where given integers, unknown integer. Required to find the desired value , which lies in the interval (as on the real line, it is clear there can be infinitely many solutions that are different to each other , where any integer). If the solution is not unique, then we will see how to get all the solutions. The decision by finding the inverse element Let us first consider the simpler case where and are relatively prime . Then we can find the inverse of a number , and multiplying on both sides of it, to obtain a solution (and it will be the only ): Now consider the case and are not relatively prime . Then, obviously, the decision will not always exist (for example, ). Suppose , ie their greatest common divisor (which in this case is greater than one). Then, if not divisible , the solutions do not exist. In fact, if any left side of the equation, i.e. , is always divisible by , while the right part it is not divided, which implies that there are no solutions. If divided into , then dividing both sides by it (ie, dividing , and on ), we arrive at a new equation: where and are already relatively prime, and this equation we have learned to solve. We denote its solution through . Clearly, this will also be a solution of the original equation. If, however , it is not the only solution. It can be shown that the original equation has exactly solutions, and they will look like: Summarizing, we can say that the number of solutions of linear modular equations is either or zero. Solution using the Extended Euclidean algorithm We give our modular equation to Diophantine equation as follows: where and unknown integers. Way of solving this equation is described in the relevant article of linear Diophantine equations of the second order , and it is in the application of the Extended Euclidean algorithm . There is also described a method of obtaining all solutions of this equation one solution found, and, by the way, this method on closer examination is absolutely equivalent to the method described in the preceding paragraph. Chinese remainder theorem Formulation In its modern formulation of the theorem is as follows: Let , where pairwise relatively prime. We associate with an arbitrary number of tuple where : Then this correspondence (between numbers and tuples) will be one to one . And, moreover, the operations performed on the number , you can perform the equivalent of the corresponding element of the tuple by independent operations of each component. That is, if then we have: In its original formulation of this theorem was proved by the Chinese mathematician Sun Tzu around 100 AD Namely, it is shown in the particular case is equivalent to solving a system of modular equations and solutions of the modular equation (see. Corollary 2 below). Corollary 1 Modular system of equations: has a unique solution modulo . (As above , the numbers are relatively prime, and a set an arbitrary set of integers) Corollary 2 The consequence is the relationship between the system of modular equations and one corresponding modular equation: Equation: equivalent to the system of equations: (As above, it is assumed that , the number of pairwise relatively prime, and an arbitrary integer) Garners algorithm Of the Chinese remainder theorem, it follows that you can replace operations on the number of operations on tuples. Recall, each number is assigned a tuple , where: It can be widely used in practice (in addition to the direct application for the restoration of its residues on the different modules) because we thus can replace surgery in a long arithmetic operations with an array of short numbers. For example, an array of elements enough to the number of approximately signs (if selected as the first s simple); and if selected as the s simple about a billion, then enough already with the number of about signs. But, of course, then you need to learn how to restore the number on the motorcade. From Corollary 1 shows that a recovery is possible, and only one (provided ). Garner algorithm is an algorithm that allows to perform this restoration, and quite effectively. We seek a solution in the form of: ie in a mixed radix digits with weights . We denote by ( , ) numbers is the inverse modulo (finding the inverse elements in the ring modulo described here : Substituting the expression in a mixed radix in the first equation, we get: We now substitute in the second equation: We transform this expression by taking away of both sides and dividing by : Substituting into the third equation, we obtain a similar manner: Already clearly visible pattern, which is the easiest way to express code: for (int i=0; i

Ngày đăng: 11/05/2015, 05:39

Từ khóa liên quan

Mục lục

  • MỤC LỤC

  • Algebra (23)

    • basic algorithms (20)

    • complex algorithms (3)

    • Binary exponentiation

      • Algorithm

      • Implementation

      • Examples of solving problems

        • Efficient computation of Fibonacci numbers

        • Erection reshuffle of th degree

        • Rapid application of a set of geometric operations to points

        • The number of paths in a fixed length column

        • Variation binary exponentiation: multiplication of two integers modulo

        • Tasks in the online judges

        • Sieve of Eratosthenes

          • Implementation

          • Asymptotics

          • Various optimizations sieve of Eratosthenes

            • Sifting easy to root

            • Sieve only in odd numbers

            • To reduce the amount of memory consumed

            • Block sieve

            • Upgrade to linear time work

            • Fibonacci numbers

              • Determination

              • History

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan