AC代码
1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <iostream> 6 #include <sstream> 7 #include <algorithm> 8 #include <string> 9 #include <queue> 10 #include <map> 11 #include <vector> 12 using namespace std; 13 const int maxn = 1e6+50; 14 const int inf = 0x3f3f3f3f,mod = 1000000007; 15 const double epx = 1e-10; 16 typedef long long ll; 17 ll poww(ll n,ll m) 18 { 19 ll ans = 1; 20 while(m > 0) 21 { 22 if(m & 1)ans = (ans * n) % mod; 23 m = m >> 1; 24 n = (n * n) % mod; 25 } 26 return ans; 27 } 28 ll sum(ll a,ll n) 29 { 30 if(n==1) return a; 31 ll t=sum(a,n/2); 32 if(n&1) 33 { 34 ll cur=poww(a,n/2+1)%mod; 35 t=(t+(t*cur)%mod)%mod; 36 t=(t+cur)%mod; 37 } 38 else 39 { 40 ll cur=poww(a,n/2)%mod; 41 t=(t+(t*cur)%mod)%mod; 42 } 43 } 44 int main() 45 { 46 ll a,n; 47 cin>>a>>n; 48 cout<<sum(a,n)<<endl; 49 }
参考博客 https://blog.csdn.net/acdreamers/article/details/7851144