[https://blog.csdn.net/nolanddream/article/details/44086021]
[http://codeforces.com/contest/166/problem/E]
一个正四面体顶点为A,B,C,D,从D出发,每走一步,更变当前所在顶点(不能保持不变),给定一个数 n ,求能有几种不同路径使得第 n 步走到 D。
#include
const int mod = (int)1e9 + 7;
const int maxn = (int)1e7 + 5;
long long a[maxn] = {0,0,3};//a[1]=0,a[2]=3
int main() {
int n;
scanf("%d", &n);
int i;
for (i = 3; i <= n; i++)
a[i] = (2 * a[i - 1] + 3 * a[i - 2])%mod;
printf("%I64d\n", a[n]);
}
#include
const int mod = (int)1e9 + 7;
const int maxn = (int)1e7 + 5;
int main() {
int n;
scanf("%d", &n);
int i;
long long a=1,b=0,c=0;//a[0]=1,a[1]=0
for (i = 2; i <= n; i++){
c = (2 * b + 3 * a)%mod;//计算a[n]
a=b;//更新a[n-2]
b=c;//更新a[n-1]
}
printf("%I64d\n", c);
}
#include
const int mod = (int)1e9 + 7;
const int inv4 = (mod + 1) / 4;//4的逆元
long long qpow(long long c, int n) {//快速幂
long long ans=1;
while (n > 0) {
if (n & 1)
ans =ans*c%mod;
c=c*c%mod;
n >>= 1;
}
return ans;
}
int solve(int n) {
long long ans;
if (n & 1)
ans = (3 * (qpow(3, n - 1) - 1))%mod * inv4 %mod;
else
ans = (3 * (qpow(3, n - 1) + 1))%mod * inv4 %mod;
return (int)ans;
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", solve(n));
}
#include
const int mod = (int)1e9 + 7;
int d[(int)1e7+5][4];//0=A,1=B,2=C,3=D
int main() {
int n;
scanf("%d", &n);
d[1][0] = d[1][1] = d[1][2] = 1;
for (int i = 1; i <= n; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++)
if (j != k)//不能不动
d[i][j] = (d[i][j] + d[i - 1][k]) % mod;
printf("%d", d[n][3]);
}