求:3^0 + 3^1 +...+ 3^(N) mod 1000000007
Input
输入一个数N(0 <= N <= 10^9)
Output
输出:计算结果
Input示例
3
Output示例
40
#include <bits/stdc++.h>
const int mod=1000000007;
using namespace std;
typedef long long ll;
ll x,y;
ll ex_gcd(ll a, ll b){
if(b==0){
x=1;
y=0;
return a;
}
int gcd;
gcd =ex_gcd(b,a%b);
int temp ;
temp = x,
x = y;
y=temp-(a/b)*y;
return gcd;
}
ll q_pow(ll a,ll b,ll mod){
ll ans = 1;
while(b){
if(b&1){
ans = ans * a % mod;
}
b>>=1;
a = a * a % mod;
}
return ans;
}
int main(){
ll N;
cin>>N;
//1求逆元
ex_gcd(2,mod);
if(x<0){
x+=mod;
}
ll ans = (q_pow(3,N+1,mod)-1)*x%mod;
printf("%lld",ans);
return 0;
}