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

时间:2021-11-27 15:35:13

求: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=;
while(n)
{
if(n&) ans=ans*x%MOD;
n>>=;
x=x*x%MOD;
}
return (ans+MOD-)%MOD;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=,y=;
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(,n+)*inv(,MOD)%MOD);
return ;
}