当求a的b次幂%p时,如果在求b的时候被迫要取余的话,那么就要用到欧拉降幂
就像这个是的就是在就fn的时候不得不取余,因为fn是指数上的如果直接取余的话肯定会wa,所以要用到欧拉降幂,就是在求fn的时候mod(1e9+7-1)
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int mod=1e9+7; const int maxn=1e6+100; ll f[maxn]; ll a,b,m,n; ll qpow(ll a,ll b){ ll ans=1; a%=mod; while(b){ if(b&1){ ans=(ans*a)%mod; } a=(a*a)%mod; b/=2; } return ans; } void inint(){ f[1]=1; f[2]=1; for(int i=3;i<=110000;i++){ f[i]=(a*f[i-1]+b*f[i-2])%(mod-1); } } int main(){ cin>>a>>b>>m>>n; inint(); ll ans=qpow(m,f[n])%mod; cout<<ans<<endl; }
Description
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).
Input
There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
Output
For each testcase, output an integer, denotes the result of A^B mod C.
Sample Input
3 2 4 2 10 1000
Sample Output
1 24
Hint
Source
FZU 2009 Summer Training IV--Number Theory
题意:A^B mod C
题解:
降幂公式 phi() 为欧拉函数
#include<iostream> #include<cstring> #include<cstdio> #define ll __int64 #define mod 10000000007 using namespace std; char a[1000006]; ll x,z; ll quickpow(ll x,ll y,ll z) { ll ans=1; while(y) { if(y&1) ans=ans*x%z; x=x*x%z; y>>=1; } return ans; } ll phi(ll n) { ll i,rea=n; for(i=2;i*i<=n;i++) { if(n%i==0) { rea=rea-rea/i; while(n%i==0) n/=i; } } if(n>1) rea=rea-rea/n; return rea; } int main() { while(scanf("%I64d %s %I64d",&x,a,&z)!=EOF) { ll len=strlen(a); ll p=phi(z); ll ans=0; for(ll i=0;i<len;i++) ans=(ans*10+a[i]-'0')%p; ans+=p; printf("%I64d ",quickpow(x,ans,z)); } return 0; }