Super A^B mod C FZU - 1759 (欧拉降幂入门)

具体使用方法:

题目链接:

http://acm.fzu.edu.cn/problem.php?pid=1759

题目大意:

欧拉降幂裸题

(为啥用%64d 输入是对的,用%lld 输入却是TLE。。)

AC代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<bitset>
 4 #include<cstring>
 5 using namespace std;
 6 # define ll __int64
 7 # define LL_inf (1ll<<60)
 8 # define inf 0x3f3f3f3f3
 9 const int maxn = 2e6+20;
10 ll ph(ll x)
11 {
12     ll res=x,a=x;
13     for(ll i=2; i*i<=x; i++)
14     {
15         if(a%i==0)
16         {
17             res=res/i*(i-1ll);
18             while(a%i==0)
19                 a/=i;
20         }
21     }
22     if(a>1ll)
23         res=res/a*(a-1ll);
24     return res;
25 }
26 ll qsm(ll t1,ll t2,ll mod)
27 {
28     ll ans=1ll;
29     while(t2)
30     {
31         if(t2&1)
32             ans=ans*t1%mod;
33         t1=t1*t1%mod;
34         t2>>=1;
35     }
36     return ans%mod;
37 }
38 //ll cal(ll n){
39 //if(n==1)return 0;
40 //ll tmp=ph(n);
41 //return qsm(2ll,cal(tmp)+tmp,n);
42 //}
43 char str[maxn];
44 int main()
45 {
46     ll a,c;
47     while(scanf("%I64d %s %I64d",&a,str,&c)!=EOF)
48     {
49         ll tmp=ph(c);
50         ll ans=0;
51         int len=strlen(str);
52         for(int i=0; i<len; i++)
53         {
54             ans=(ans*10+str[i]-'0')%tmp;
55         }
56        // ans+=tmp;
57         printf("%lld\n",qsm(a,ans+tmp,c));
58     }
59     return 0;
60 }

猜你喜欢

转载自www.cnblogs.com/letlifestop/p/11008687.html