题目链接

昨天上随机信号分析讲马氏链的时候突然想到这题的解法,今天写一下

定义矩阵A,Ans=A^n,令A[i][j]表示,经过1次变换后,第i个位置上的机器人位于第j个位置的情况数,则Ans[i][j]表示最初在第i个位置上的机器人n次变换后位于第j个位置的情况数

最后求一下任意两个机器人不在相同位置的情况数之和(注意乘法原理和加法原理的应用)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL; const int N=;
const LL mod=1e9+; LL hh[N][N]= {{,,,},
{,,,},
{,,,},
{,,,}
}; struct Mat
{
LL mat[N][N];
Mat()
{
memset(mat,,sizeof(mat));
}
LL* operator [](int x) //注意这种写法
{
return mat[x];
}
} A;
Mat Mut(Mat a,Mat b)
{
Mat c;
for(int k=; k<N; k++)
for(int i=; i<N; i++)
for(int j=; j<N; j++)
{
c[i][j]+=a[i][k]*b[k][j]%mod;
c[i][j]=c[i][j]%mod;
}
return c;
}
Mat Qpow(Mat a,LL n)
{
Mat c;
for(int i=; i<N; ++i)
c[i][i]=;
for(; n; n>>=)
{
if(n&) c=Mut(c,a);
a=Mut(a,a);
}
return c;
} void init_A()
{
for(int i=; i<N; i++)
for(int j=; j<N; j++)
A[i][j]=hh[i][j];
} int main()
{
LL n,Fn,Gn;
init_A();
while(cin>>n)
{
Mat Ans=Qpow(A,n);
LL sum=;
for(int i1=; i1<; i1++)
for(int i2=; i2<; i2++)
for(int i3=; i3<; i3++)
for(int i4=; i4<; i4++)
if(i1!=i2&&i1!=i3&&i1!=i4&&i2!=i3&&i2!=i4&&i3!=i4)
{
sum+=Ans[][i1]*Ans[][i2]%mod*Ans[][i3]%mod*Ans[][i4]%mod;
sum%=mod;
}
cout<<sum<<endl;
}
}

51nod 1122:机器人走方格 V4 (矩阵快速幂)的更多相关文章

  1. 51nod 1122 机器人走方格 V4 【矩阵快速幂】

    首先建立矩阵,给每个格子编号,然后在4*4的格子中把能一步走到的格子置为1,然后乘n次即可,这里要用到矩阵快速幂 #include<iostream> #include<cstdio ...

  2. 1122 机器人走方格 V4

    1122 机器人走方格 V4 基准时间限制:1 秒 空间限制:131072 KB  四个机器人a b c d,在2 * 2的方格里,一开始四个机器人分别站在4个格子上,每一步机器人可以往临近的一个格子 ...

  3. 51nod1122 机器人走方格 V4

    矩阵快速幂求出每个点走n步后到某个点的方案数.然后暴力枚举即可 #include<cstdio> #include<cstring> #include<cctype> ...

  4. 51nod 1118 机器人走方格 解题思路:动态规划 & 1119 机器人走方格 V2 解题思路:根据杨辉三角转化问题为组合数和求逆元问题

    51nod 1118 机器人走方格: 思路:这是一道简单题,很容易就看出用动态规划扫一遍就可以得到结果, 时间复杂度O(m*n).运算量1000*1000 = 1000000,很明显不会超时. 递推式 ...

  5. 51nod 1119 机器人走方格 V2

    1119 机器人走方格 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少 ...

  6. 51nod 1120 机器人走方格V3

    1120 机器人走方格 V3  基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 N * N的方格,从左上到右下画一条线.一个机器人从左上走到右下,只 ...

  7. 51Nod 1118 机器人走方格--求逆元

    (x/y) %mod =x*(y^(mod-2))%mod; 在算x,y的时候可以一直mod 来缩小 y^(mod-2)显然是个快速幂 #include <iostream> #inclu ...

  8. 51nod_1122:机器人走方格 V4 (矩阵快速幂)

    题目链接 昨天上随机信号分析讲马氏链的时候突然想到这题的解法,今天写一下 定义矩阵A,Ans=A^n,令A[i][j]表示,经过1次变换后,第i个位置上的机器人位于第j个位置的情况数,则Ans[i][ ...

  9. 51Nod——N1118 机器人走方格

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1118 基准时间限制:1 秒 空间限制:131072 KB 分值: 0  ...

随机推荐

  1. linux发版

    使用MobaXterm_v8.5 1.upload需要更换的文件 2.关闭Tomcat    cd /usr/local/tomcat/apache-tomcat-6.0.30-pcitcaly/  ...

  2. (转)k8s集群部署二:flannel网络

    转:https://blog.csdn.net/sinat_35930259/article/details/79946146 Overlay Network模式 覆盖网络,在基础网络上叠加的一种虚拟 ...

  3. Hadoop学习之路(二)HDFS基础

    1.HDFS前言 HDFS:Hadoop Distributed File System,Hadoop分布式文件系统,主要用来解决海量数据的存储问题. 设计思想 分散均匀存储 dfs.blocksiz ...

  4. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  5. Layui数据表格/搜索重加载/分条件操作/工具条监听

    <div class="layui-layout layui-layout-admin" style="padding-left: 20px;"> ...

  6. lnmp环境下 tp3.2 not found

    最近将一个lamp环境下使用tp3.2 开发的项目迁移到本地了, 但是在打开项目的时候,提示 not found,经过多方面查找发现是伪静态问题,解决方法如下: 在nginx 域名配置文件我这里是[v ...

  7. Windows系统命令整理-Win10

    硬件相关 显卡 显卡升级 - 我的电脑->属性->设备管理器->显示适配器->更新驱动程序 服务 telnet 安装:启用或关闭Windows 功能,勾选上“Telnet客户端 ...

  8. Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)

    传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...

  9. mysql: "Warning: Using a password on the command line interface can be insecure." 解决方法

    错误重现: 命令行或者shell脚本中执行以下命令,如果您当前服务器mysql版本是大于5.6的,则会出现警告:Warning: Using a password on the command lin ...

  10. jquery实现全选,反选,取消的操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...