Partial knowledge of number theory

Fermat's theorem:

a p ≡a (mod p)

where p is a prime number and a is not a multiple of p

Prove:

。。。。。

 

Euler's theorem:

aφ(p)≡1(mod p)

φ(x ) (Eulerian function) is the number of numbers less than or equal to x and relatively prime to x

φ(x)=∏(pi-1)*p i ki-1  where pi represents the prime factor of x, and ki represents the number of such prime factors

Especially for prime numbers φ(x)=x-1.

The code implementation of the Euler function:

 1 #include<cstdio>
 2 #include<Iostream>
 3 using namespace std;
 4 int ol(int x)
 5 {
 6     int ans=1;
 7     for(int i=2;i*i<=x;++i)
 8     {
 9         if(x%i==0)
10         {
11             x/=i;
12             ans*=i-1;
13         }
14         while(x%i==0)
15         {
16             x/=i;
17             ans*=i;
18         }
19     }
20     if(x>1) ans*=x-1;
21     return ans;
22 }
23 int main()
24 {
25     int a;
26     scanf("%d",&a);
27     printf("%d",ol(a));
28     return 0;
29 }

In the last function, if x>1, ans*=x-1 made me confused at first, and then I thought, if this number divides all prime factors once, if the remaining number is not 1, then the remaining number There must be only one and a prime number of (the proof is obvious)

 

 

 Sieve

There are two sieve methods, the first is called Sieve of Eratosthenes (complexity O(n log logn)), and the other is Euler sieve (complexity O(n))

The sieve of Eratosthenes is actually to use the obtained prime numbers to mark all his multiples within n as composite numbers, and the last remaining number is the composite number.

While performing the sieve method, you can find the smallest prime factor of each number by the way (that is, the prime number that updates him for the first time)

 

Euler sieve method: In the Ehrlich sieve method, each composite number may be updated many times, which is not necessary, so there is an Euler sieve.

The idea of ​​Euler's sieve is to ensure that each composite number is only sieved by its smallest prime factor. Use an array dis to store the obtained prime numbers,

Then use the prime numbers that have been obtained to sieve other numbers. The main place is  if(i%dis[j]==0) break;  in the code.

Code for two sieves

 

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 const int N=10000;
 6 void aa(int n);
 7 void ol(int n);
 8 int main()
 9 {
10     int n;
11     scanf("%d",&n);
12     aa(n);
13     printf("\n");
14     ol(n);
 15  }
 16  
17  void aa( int n) // Earth sieve method 
18  {
 19      int vis[N];
 20      int dis[N]; // Used to store the smallest prime factor 
21      for ( int i = 2 ;i<=n;++ i)
 22      {
 23          if (! vis[i])
 24          {
 25              for ( int j=i* 2 ;j<=n;j+= i)
 26              {
 27                  if (! vis[j])
28                 {
29                     vis[j]=1;
30                     dis[j]=i;
31                 }
32             }
33         }
34     }
35     for(int i=1;i<=n;++i)
36     {
37         if(!vis[i])
38         {
39             printf("%d ",i);
40         }
41     }
42 }
43 void ol(int n)
44 {
45     int vis[N];
46     int dis[N],js=0;
47     for(int i=2;i<=n;++i)
48     {
49         if(!vis[i])
50         {
51             dis[++js]=i;
52         }
53         for(int j=1;dis[j]*i<=n;++j)
54         {
55             vis[i*dis[j]]=1;
56             if(i%dis[j]==0) break;
57         }
58     }
59     for(int i=1;i<=n;++i)
60     {
61         if(!vis[i]) printf("%d ",i);
62     }
63 }

 

 Fast Powers and Fast Multiplications

 Although fast exponentiation and fast multiplication seem to have nothing to do with it, there are not many things, so let's sort them out together

The quick exponentiation idea is to multiply a x as x a, use now to record the current answer, then divide the exponent by 2 each time, and then square the current answer, if the last digit of the binary of x is 1, then Multiply the answer by the current number. Fast multiplication is similar, just treats a*x as the addition of x a's.

code

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 int mi(int a,int x)
 5 {
 6     int ans=1;
 7     for(int now=a;x>=1;x>>=1,now=now*now)//a表示底数,x表示次数 
 8     {
 9         if(x&1) ans=ans*now;
10      } 
11      return ans;
12 }
13 int cheng(int a,int x)//表示a*x 
14 {
15     int ans=0;
16     for(int now=a;x>=1;now=now*2,x>>=1)
17     {
18         if(x&1) ans=ans+now;
19     }
20     return ans;
21 }
22 int main()
23 {
24     int a,x;
25     scanf("%d%d " ,&a,& x);
 26      printf( " Fast power %d\n " ,mi(a,x));
 27      printf( " Fast multiplication %d\n " ,cheng(a,x) );
 28      return  0 ;
 29 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292733&siteId=291194637