## 51Nod1013 multiplicative inverse and inverse fast operation

Requirements: (3 ^ 0 + 3 ^ 1 + ... + 3 ^ (N)) mod 1000000007

Input

Enter a number N (0 <= N <= 10 ^ 9)

Output

Output: the results

``````Sample Input
3
Sample Output
40``````

Timeout Code:

``````#include<iostream>
using namespace std;
//MOD=1000000007;
//pow(x,n)%mod
typedef long long ll;
ll mod_pow(ll x,ll n,ll mod){//时间复杂度O(logn)
ll ans=1;
while(n>0){
if(n&1)
ans=ans*x%mod;
x=x*x%mod;
n>>=1;
}
return ans;
}
int main(){
int n,ans=0;
cin>>n;
for(int i=0;i<=n;i++){
ans+=mod_pow(3,i,1000000007);
ans=ans%1000000007;
}
cout<<ans<<endl;
}
/*Sample Input
3
Sample Output
40*/``````

AC Code

``````#include <iostream>
using namespace std;
typedef long long ll;
ll MOD = 1000000007;
ll mod_pow(ll n){
ll k=3,ans=1;
while(n){
if(n&1)
ans=(ans*k)%MOD;
n/=2;
k=(k*k)%MOD;
}
return ans;
}
int main(){
ll n;
while (cin>>n){
ll ans=((mod_pow(n+1)-1)*500000004)%MOD;
cout<<ans<<endl;
}
}``````

Can refer to the blog links: https://blog.csdn.net/JinbaoSite/article/details/72676514

Published 73 original articles · won praise 27 · views 1256

### Guess you like

Origin blog.csdn.net/queque_heiya/article/details/103772064
Recommended
Ranking
Daily