快速幂学习
#include<bits/stdc++.h>
using namespace std;
//递归快速幂
int fastpow(int a,int n){
if(n==1) return a;
int temp=fastpow(a,n/2);
if(n%2==1)return temp*temp*a;
else return temp*temp;
}
//位运算快速幂
int fastpow1(int a,int n){
int base=a,res=1;
while(n){
if(n&1)res*=base;
base*=base;
n>>=1;
}
return res;
}
int main(){
int a,n;
cin>>a>>n;
return cout<<fastpow1(a,n)<<endl,0;
}
例 hdu 2817
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 200907;
ll pow_mod(long long a,long long b){
ll ret=1;
a%=mod;
while(b){
if(b&1)ret=ret*a%mod;
a=(a*a)%mod,b>>=1;
}
return ret%mod;
}
int main(){
int t;
cin>>t;
while(t--){
ll a1,a2,a3,k;
cin>>a1>>a2>>a3>>k;
a3-a2==a2-a1?cout<<(a1%mod+(((k-1)%mod)*((a2-a1)%mod))%mod)%mod<<endl:cout<<((a1%mod)*pow_mod(a2/a1,k-1))%mod<<endl;
}
return 0;
}