Try Again

51Nod 3的幂的和(扩展欧几里德求逆元)

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

Input
输入一个数N(0 <= N <= 10^9)
Output
输出:计算结果
Input示例
3
Output示例
40

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
ll n;
ll quick_pow(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=ans*x%MOD;
        n>>=1;
        x=x*x%MOD;
    }
    return (ans+MOD-1)%MOD;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    ll d=exgcd(b,a%b,x,y);
    ll t=x;
    x=y;
    y=t-a/b*y;
    return d;
}
ll inv(ll a,ll mod)
{
    ll x,y;
    exgcd(a,mod,x,y);
    return (mod+x%mod)%mod;
}
int main()
{
    scanf("%lld",&n);
    printf("%lld\n",quick_pow(3,n+1)*inv(2,MOD)%MOD);
    return 0;
}

 

posted @ 2018-05-01 15:54  十年换你一句好久不见  阅读(279)  评论(0编辑  收藏  举报