Description

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

 

Input

每行一个整数N(0 <= N <= 10^9)

 

Output

输出:计算结果

 

Sample Input

3

Sample Output

40

 

3的幂的和

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
inline int read()
{
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
const int 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,m;
    while(cin>>n){
    m=(mod+1)/2;
    ll ans=((mod_pow(n+1)-1)*m)%mod;
    cout<<ans<<endl;
    }
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-14
  • 2021-10-01
  • 2022-01-14
  • 2021-11-19
  • 2021-08-03
  • 2021-11-27
  • 2021-07-19
相关资源
相似解决方案