ZCMU 1598-TomCat的环

Description

TomCat有一个环(如图)有N个单元,并且有4中颜色。他希望把环的每一个单元格都染上颜色,但是相邻的两个单元格颜色不能相同。他想知道一共有几种染色方法

 

 

Input

输入单元格数N,N<=100000。

Output

输出染色总数对1000000007取余的结果。

Sample Input

1 2

Sample Output

4 12

HINT

快速幂优化一下就好。

数学原理是:对分成了n块的环,涂m种颜色,相邻两个不能为同种色,

那么涂色方案数ans = (m-)^n+(-1)^n*(m-1);(m>=2&&n>=2)。

AC代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll mod = 1000000007;

ll quick_pow(ll a, ll b)

{

ll ans = 1;

a = a % mod;

while (b)

{

if (b & 1)

ans = ans * a%mod;

a = a * a%mod;

b >>= 1;

}

return ans;

}

int main()

{

int n;

while (~scanf("%d", &n))

{

if (n == 1)printf("4\n");

else if (n == 2)printf("12\n");

else

{

if (n % 2)

printf("%lld\n",quick_pow(3, n) % mod - 3);

else printf("%lld\n", quick_pow(3, n) % mod + 3);

}

}

return 0;

}

猜你喜欢

转载自blog.csdn.net/Small_Orange_glory/article/details/81231812