H. Coloring Game (南昌网络赛)

探讨了在2xN网格上,从左上角到右下角移动并涂色的算法问题,通过分析不同路径的选择,计算出所有可能的颜色方案数量,并使用快速幂进行高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

David has a white board with 2 \times N2×N grids.He decides to paint some grids black with his brush.He always starts at the top left corner and ends at the bottom right corner, where grids should be black ultimately.

Each time he can move his brush up(), down(), left(), right(), left up(), left down(), right up(), right down () to the next grid.

For a grid visited before,the color is still black. Otherwise it changes from white to black.

David wants you to compute the number of different color schemes for a given board. Two color schemes are considered different if and only if the color of at least one corresponding position is different.

Input

One line including an integer n(0<n \le 10^9)n(0<n≤109)

Output

One line including an integer, which represent the answer \bmod 1000000007mod1000000007

样例输入1

2

样例输出1

4

样例解释1

样例输入2

3

样例输出2

12

样例解释2

 题目链接:https://nanti.jisuanke.com/t/38227

代码:

做的时候想了各种做法,然后发现就是一道找规律的题。

 

 

 

 

 

 

 

 

 

用2*4举例,x1和yn肯定是要走的,是黑色,则y1可以走可以不走,yn也一样,则有2*2种选择。则除两边外的中间的2至n-1行中,每一列有三种情况,上黑下白,下白上黑,或者上黑下黑。所以2至n-1行有3^(n-2)种情况。一个2*n的共有2*2*3^(n-2)种情况。然后注意数据范围,要用快速幂计算。

#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL fun(LL x,LL n)
{
    LL res=1;
    while(n>0)
    {
        if(n & 1)
            res=(res*x)%1000000007;
        x=(x*x)%1000000007;
        n>>= 1;
    }
    return res;
}
int main()
{
    long long n;
    scanf("%lld",&n);
    if(n==1)
    {
        printf("1\n");
        return 0;
    }
    long long sum=fun(3,(n-2))*4;
    printf("%lld\n",sum%1000000007);
    return 0;
}

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值