[Number theory] - Euler's theorem

Today, fishing in troubled waters to see a question:

 

 This is not a power of quick questions you naked? ?

Then a look at the data range:

 

 ? ? ? This range of b scares me.

After some study, the original study this question are:

Euler's theorem & Extended Euler's theorem

Proof skip directly to the conclusion:

 

 

 

 (图源 OI wiki)

So this question is to first deal with the Euler function, and then expand solved according to Euler. Note b to side modulo input side .

Euler handling functions:

  Screen similar linear prime, you can see standard process, well understood.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll M=1e6+10;
 5 ll ans[M],phi[M];
 6 ll cnt;
 7 ll m,a,b;
 8 void Eulersieve(){
 9     phi[1]=1;
10     for(ll i=2;i<=m;i++){
11         if(!phi[i]){
12             for(ll j=i;j<=m;j+=i){
13                 if(!phi[j]) phi[j]=j;
14                 phi[j]=phi[j]/i*(i-1);
15             }
16         }
17     }
18 } 
19 ll read(){
20     ll x=0,f=1;
21     char c=getchar();
22     while(!isdigit(c)){
23         if(c=='-') f=-1;
24         c=getchar();
25     } 
26     while(isdigit(c)){
27         x=x*10+c-'0';
28         c=getchar();
29     }
30     return x*f;
31 }
32 ll getb(){
33     ll x=0,flag=0;
34     char c=getchar();
35     while(!isdigit(c)){
36         c=getchar();
37     }
38     while(isdigit(c)){
39         x=(x*10+c-'0');
40         if(x>=phi[m]){
41             flag=1;
42             x%=phi[m];
43         }
44         c=getchar();
45     }
46     if(x>=phi[m]){
47         flag=1;
48         x%=phi[m];
49     }
50     return flag==1?x+phi[m]:x;
51 }
52 ll qp(ll n,ll p){
53     ll res=n,ans=1;
54     while(p){
55         if(p&1){
56             ans=(res*ans)%m;
57         }
58         res=(res*res)%m;
59         p>>=1;
60     }
61     return ans;
62 }
63 int main(){
64     a=read();
65     m=read();
66     Eulersieve();
67     b=getb();
68     printf("%lld",qp(a,b));
69     return 0;    
70 }

Guess you like

Origin www.cnblogs.com/Nelson992770019/p/11517149.html