luogu 5091 欧拉定理

 

 

裸题: 扩展欧拉定理的应用 

 1 #include<bits/stdc++.h>
 2 using namespace std; 
 3 int ph(int x){
 4     int ret=x;  
 5     for(int i=2;i*i<=x;i++)
 6         if(x%i==0){
 7             ret=ret/i*(i-1);    
 8             while (x%i==0) x/=i;  
 9         }
10     if(x>1) ret=ret/x*(x-1);     
11     return ret;  
12 }  
13 int main(){
14     int a,b=0,m,check=0;  
15     scanf("%d%d",&a,&m);  
16     int t=ph(m); 
17     char c;  
18     while ((c=getchar()) && !isdigit(c));  
19     while (isdigit(c)){
20         b=b*10+(c^48);  
21         while (b>=t) b-=t,check=1;    
22         c=getchar();    
23     } 
24     if(check) b=b+t;   
25     int ans=1;  
26     for(int i=0;i<=20;i++){
27         if(b&(1<<i)) ans=1LL*ans*a%m;  
28         a=1LL*a*a%m; 
29     }
30     printf("%d\n",ans);  
31     return 0; 
32 }
View Code

 

posted @ 2019-09-11 11:01  zjxxcn  阅读(139)  评论(0编辑  收藏  举报