Algorithm competition-Training Guide Chapter 2-Summary of common algorithm in Number Theory

Source: Internet
Author: User

Number theory is a magic thing. All the conclusions are classic, some understand, and some do not understand.

Next we will introduce them one by one.

First, the prime number itself is a very surprising number, because it represents a unique, and itself has a connection factor, one is 1, and the other is itself, because 1 is a factor (except 0) for each number, it is equivalent to only oneself. A person who feels good about himself!

The simplest algorithm is to find a factor from 2-sqrt (2). If it does not exist, it indicates that it is a prime number. Why to SQRT (n )? You think, SQRT (n) * SQRT (n) = N, how is the number of your factors calculated? Is it from the number of factors in 1-sqrt (n) * 2? Obviously, we can see that the number of factors is symmetric about SQRT (N). There are several factors over there, so enumeration is generally enough!

Well, Miller-Robin has previously written this algorithm, but it seems that he hasn't read it before. But one of them is often used, that is, the screening method is used to obtain the range prime number. This idea is widely used and is also used in Euler's functions.

Paste the algorithm for screening prime numbers and add Miller-ROBIN:

# Include <stdio. h> # include <string. h> # include <math. h >#include <iostream >#include <string> using namespace STD; const int maxn = 1000000 + 11; bool P [maxn]; int index [maxn]; void Init () {memset (p, 0, sizeof (p); P [0] = 1; p [1] = 1; for (INT I = 4; I <maxn; I + = 2) {P [I] = 1;} int CNT = 0; index [CNT ++] = 2; for (INT I = 3; I <(INT) SQRT (maxn * 1.0); I + = 2) {If (! P [I]) {index [CNT ++] = I; int K = I * 2; for (Int J = K; j <maxn; j + = I) {P [J] = 1 ;}}/ * For (INT I = 0; I <CNT; I ++) {printf ("% d \ n ", index [I]);} */} void sieve () // This is another method. It looks simple, but it is the same as the above idea. {int M = (INT) SQRT (n + 0.5); For (INT I = 2; I <m; I ++) {If (! P [I]) {for (Int J = I * I; j <n; j + = I) {P [J] = 1 ;}}} int main () {Init (); sieve (); System ("pause"); Return 0 ;}

Ii. Euclidean algorithm. The essence of the Euclidean algorithm is probably division. It is easy to understand. The difficulty is to extend the Euclidean Algorithm and calculate a * x + B * Y = gcd (a, B). This is the best Algorithm for Solving Linear Equations. Another good application is the multiplication inverse element. This application is very large because many times modulo has a prime number because of the large data volume. The reverse element can divide the number into multiplication, which is better.

Paste these algorithms:

#include <stdio.h>#include <string.h>#include <iostream>#include <string>using namespace std;typedef long long LL;void extgcd(LL a, LL b, LL &d, LL &x, LL &y){if (b == 0){x = 1;y = 0;d = a;}else{extgcd(b, a % b, d, y, x);y -= x * (a / b);}}LL inv(LL a, LL n){LL d, x, y;extgcd(a, n, d, x, y);return d == 1 ? (x + n) % n : -1;}int main(){int ans = inv(2, 5);cout << ans << endl;system("pause");return 0;}

Iii. Euler's function PHI (n ). First, we need to understand the meaning of the Euler's function: the number of numbers between 1-N and n-grams. The formula is

Phi (n) = N * (1-1/P1) (1-1/P2) (1-1/P3) (1-1/P4 )......

P1, P2, and P3 are all prime factors for n-factor decomposition. With this formula, it is easy to do.

Now, the code for prime factor decomposition is actually included in the euler_phi function. This idea is to remove this factor if it is included.

Prime factor decomposition:

#include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <string>using namespace std;int main(){int N;while (scanf("%d", &N) != EOF){int cnt = 0;cout << "N = ";for (int i = 2; i <= (int)sqrt(N + 0.5); i++){if (N % i == 0){cout << i << "^";while (N % i == 0){N /= i;cnt++;} cout << cnt << " ";}}if (N > 1){cout << N << "^1";}cout << endl;}system("pause");return 0;} 

Next is euler_phi:

# Include <stdio. h> # include <string. h> # include <math. h> # include <iostream> # include <string>/** first, you must know what the Euler's function formula is: * The most concise formula derived is: phi (n) = N (1-1/P1) (1-1/P2 )...... * This can be easily obtained. * PHI (n) indicates the number of integers that do not exceed X and are mutually exclusive to X. */using namespace STD; typedef long ll; const int maxn = 100000 + 11; int Phi [maxn]; int euler_phi (int n) {ll ans = N; for (INT I = 2; I <= (INT) SQRT (n + 0.5); I ++) {If (N % I = 0) {ans = ANS/I * (I-1); While (N % I = 0) {n/= I ;}} if (n> 1) {ans = ANS/N * (N-1);} return ans;} void phi_table (int n) {memset (PHI, 0, sizeof (PHI )); phi [1] = 1; for (INT I = 2 ; I <= N; I ++) // to obtain all Phi, We need to loop to N, because some of them are greater than SQRT (n) {// The prime number has not been obtained; If (! Phi [I]) {for (Int J = I; j <n; j + = I) {If (! Phi [J]) {Phi [J] = J;} Phi [J] = Phi [J]/I * (I-1 );}}}} void print (int n) {for (INT I = 1; I <= N; I ++) {printf ("Phi [% d] = % d \ n ", i, Phi [I]) ;}} int main () {// cout <"Phi (I) =" <euler_phi (3) <Endl; phi_table (30); print (30); System ("pause"); Return 0 ;}

This is just the most basic algorithm. Of course there are many algorithms in number theory. I will add them later.

4. China residue theorem. Solve multiple modulus equations, but the variable is still a problem, that is, x = A [I] (% m [I]). The method is to make m the product of all M [I]. If Wi = m/MI, then gcd (WI, mi) = 1. so that wi * P + Mi * q = 1, you can use extgcd to find the P solution for Wi, so that E = wi * Pi, then the equations are equivalent to equation x = e1 * A1 + e2 * A2 + E3 * A3 ...... Note that X is the unique solution.

The Code is as follows:

# Include <stdio. h> # include <string. h >#include <iostream >#include <string>/** use and solve the Chinese Remainder Theorem x = A [I] (% m [I]); * m [I] will find a unique partial solution for every element of the mutual element. */Using namespace STD; typedef long ll; const int mod = 1000000000 + 7; void extgcd (int A, int B, Int & D, Int & X, Int & Y) {If (B = 0) {d = A; X = 1; y = 0;} else {extgcd (B, A % B, D, Y, X ); y-= x * (a/B);} int China (INT N, int * a, int * m) {int m = 0; int X, Y, D; int ans = 0; For (INT I = 0; I <n; I ++) {M + = m [I];} For (INT I = 0; I <n; I ++) {int W = m/m [I]; extgcd (M [I], W, D, x, y ); ans = (LL) ans + (LL) y * w * A [I]) % MOD;} return (ANS + mod) % MOD;} int main () {system ("pause"); Return 0 ;}

 

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.