求:3^0 + 3^1 +…+ 3^(N) mod 1000000007
输入
输入一个数N(0 <= N <= 10^9)
输出
输出:计算结果
输入样例
3
输出样例
40
这个题一看就是等比数列,通过快速幂计算求值,
//#include<bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
//#define mp make_pair
#define pb push_back
#define lson k<<1
#define rson k<<1|1
#define mid (1+r)/2
#define ms(x, y) memset(x, y, sizeof x)
#define MOD 998244353
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll mod_pow(ll x,ll n,ll mod){
ll res = 1;
while(n>0){
if(n&1){
res = res*x%mod;
}
x = x*x%mod;
n>>=1;
}
return res;
}
int main(){
int n;
scanf("%d",&n);
int mod = 1000000007;
ll resu = mod_pow(3,n+1,mod);
if(resu&1)
printf("%lld\n",(resu-1)/2);
else
printf("%lld\n",(resu+mod-1)/2);
return 0;
}
这个主要的问题是,通过mod计算影响了结果的奇偶性,如果是奇数的话进行除法运算结果会出现余数,就会出现结果出错。