HLG 1375 The Active Leyni (快速幂,动态规划,矩阵乘法)

Description

There is a map of the town where Leyni lives.

The vertex S indicates the home of Leyni and the vertexes A, B, C indicate the homes of his friends. They are connected to each other as the map presents.

Leyni is too active that he can’t stay idle. It will count one step every time when he walks from one place to another. He starts from his home S and won’t stop.

He wonders the number of ways in which he can go from his home S to itself in exactlyn steps. The number may be quite large, you should output it modulo 1000000007.


 

Input

There are multiple test cases. The first line of input is an integerT indicating the number of test cases. Then T test cases follow.

For each test case:

Line 1. This line contains an integer n (1 ≤n ≤ 109) indicating the required steps.

Output

For each test case:

Line 1. Output the number of ways modulo 1000000007.

Sample Input

2

2

4

Sample Output

3

21

Hint

In the first sample, the possible paths are:

S=>A=>S

S=>B=>S

S=>C=>S

顶点S代表Leyni的家,顶点A,B,C代表他的3个朋友的家,题中给出数n,Leyni从自家出发访问朋友经过n步再回到自己的家,问有多少不同的走法。

动态规划:

    定义f[i][0]为走了i步恰好到达S的不同走法

    定义f[i][1]为走了i步恰好到达A的不同走法

    定义f[i][2]为走了i步恰好到达B的不同走法

    定义f[i][3]为走了i步恰好到达C的不同走法

状态转换方程

    f[i][0]=f[i-1][1]+f[i-1][2]+f[i-1][3]

    f[i][1]=f[i-1][0]+f[i-1][2]+f[i-1][3]

    f[i][2]=f[i-1][0]+f[i-1][1]+f[i-1][3]

    f[i][3]=f[i-1][0]+f[i-1][1]+f[i-1][2]

数据量很大显然不能用纯粹的递推,递推一般数据较大时都是矩阵乘法,这道题的初始矩阵是A[1,0,0,0]即第0步到各个点的走法要乘的矩阵为一个只有对角线全为0,其他皆为1的矩阵.

走n步恰好到达S点的不同走法即使求A * Bn

 

#include <iostream>
using namespace std;
#define mod 1000000007
long long n;
void mul(long long A[][4],long long B[][4])
{
    int i,j,k;
    long long C[4][4]={0};
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        for(k=0;k<4;k++)
          C[i][j]=C[i][j]+((A[i][k]%mod)*(B[k][j]%mod))%mod;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
         A[i][j]=C[i][j]%mod;
}
void martix(long long A[][4],long long B[][4])
{
    while(n)
    {
        if(n&1)
            mul(A,B);
        mul(B,B);
        n=n/2;
    }
    cout<<A[0][0]<<endl;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        long long A[4][4]={{1,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
        long long B[4][4]={{0,1,1,1},{1,0,1,1},{1,1,0,1},{1,1,1,0}};
        martix(A,B);
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/MisdomTianYa/p/6581876.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:OpenCvSharp是一个OpenCV的.Net wrapper,用于开发基于OpenCV的应用程序,它与原始的OpenCV更接近,并提供了详细的使用样例。 引用:对于使用OpenCV进行图像处理的代码示例,可以使用import numpy as np import cv2来导入OpenCV库,并使用cv2.imread、cv2.imshow等函数进行图像的读取和显示。 引用:如果想要使用OpenCV进行分类器的生成,可以使用opencv_traincascade.exe命令,并提供指定的参数,例如-data用于指定生成的分类器的保存路径,-vec用于指定正样本描述文件的路径,-bg用于指定负样本文件的路径,以及其他参数如numPos、numNeg、minHitRate等。 关于"opencv hlg"的问题,根据提供的引用内容,我没有找到与"opencv hlg"相关的具体信息。可能需要提供更多背景或上下文信息来解答该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [OpenCvSharp](https://download.csdn.net/download/qq_18865111/86722032)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [python opencv 读取图片 存储图片](https://blog.csdn.net/weixin_41799483/article/details/80829825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [opencv分类器训练方法](https://blog.csdn.net/weixin_41799483/article/details/80567909)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值