基础矩阵快速幂何必看题解

#include <bits/stdc++.h>
using namespace std; /*
0 1 2 3 4 5 6 7
0 0 0 */
const int mod=10007;
struct asd{
int num[4][4];
}; asd mul(asd a,asd b)
{
asd ans;
memset(ans.num,0,sizeof(ans.num));
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
for(int k=0;k<4;k++)
ans.num[i][j]=(ans.num[i][j]+a.num[i][k]*b.num[k][j]%mod)%mod;
return ans;
} asd quickmul(int g,asd x)
{
asd ans;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(i==j)
ans.num[i][j]=1;
else
ans.num[i][j]=0;
}
while(g)
{
if(g%2)
ans=mul(ans,x);
x=mul(x,x);
g>>=1;
}
return ans;
} int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
int a,b,c,n;
scanf("%d%d%d%d",&n ,&a ,&b ,&c);
printf("Case %d: ",cas++);
if(n<=2)
{
puts("0");
continue;
}
asd tmp;
tmp.num[0][0]=a;tmp.num[0][1]=0;tmp.num[0][2]=b;tmp.num[0][3]=c;
tmp.num[1][0]=1;tmp.num[1][1]=0;tmp.num[1][2]=0;tmp.num[1][3]=0;
tmp.num[2][0]=0;tmp.num[2][1]=1;tmp.num[2][2]=0;tmp.num[2][3]=0;
tmp.num[3][0]=0;tmp.num[3][1]=0;tmp.num[3][2]=0;tmp.num[3][3]=1; asd ans=quickmul(n-2,tmp);
printf("%d\n",ans.num[0][3]);
}
return 0;
}

lightoj 1096【矩阵快速幂(作为以后的模板)】的更多相关文章

  1. hdu 5015 矩阵快速幂(可用作模板)

    转载:http://blog.csdn.net/wdcjdtc/article/details/39318847 之前各种犯傻 推了好久这个东西.. 后来灵关一闪  就搞定了.. 矩阵的题目,就是构造 ...

  2. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  3. UVA - 11149 (矩阵快速幂+倍增法)

    第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio ...

  4. LightOj 1096 - nth Term (矩阵快速幂,简单)

    题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...

  5. LightOj 1065 - Number Sequence (矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...

  6. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  7. LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...

  8. LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)

    题意:给出一个字符集和一个字符串和正整数n,问由给定字符集组成的所有长度为n的串中不以给定字符串为连续子串的有多少个? 析:n 实在是太大了,如果小的话,就可以用动态规划做了,所以只能用矩阵快速幂来做 ...

  9. hdu 1575 Tr A(矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term  类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...

随机推荐

  1. erlang防止反编译

    前面提到了erlang的反编译,下面说下防止反编译: 1)建立~/.erlang.crypt 在编译的用户名的home目录中建立一个加密方法的文件.erlang.crypt,内容如下: [{debug ...

  2. Redis 过期键的设置、获取和删除过期时间

    Redis 过期键的设置.获取和删除过期时间 转自http://blog.51cto.com/littledevil/1813956 设置过期 默认情况下键是没有生存时间的,也就是永不过期,除非清空内 ...

  3. 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)

    主要内容: 1.基于CNN的mnist手写数字识别(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...

  4. 在苹果iOS平台中获取当前程序进程的进程名等信息

    本文由EasyDarwin开源团队成员Penggy供稿: Objective-C 提供 NSProcessInfo 这个类来获取当前 APP 进程信息, 然而我们的静态库是 pure C++ 工程. ...

  5. Vue入门(一) 环境配置

    Node.js 安装,https://nodejs.org/en/  默认安装就可以 安装好后测试版本,cmd  键入命令 1.node -v 2.npm -v 安装,淘宝 NPM         n ...

  6. IE6 PNG图片不透明的解决方案-tinypng

    https://tinypng.com/ 把图片上传上去,就能处理这个问题啦. 纠正一下 再也不用把png图片一个个拖到TinyPNG进行在线压缩(和熊猫哥哥说再见了):再不用把JPG/JPEG图片拖 ...

  7. Android junit4 单元测试 cant open database错误 获取context上下文问题

    Context context = getInstrumentation().getTargetContext()这样就能在data/data/包/databases下找到数据库文件了 public ...

  8. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  9. Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP

    题目链接:http://codeforces.com/contest/132/problem/C C. Logo Turtle time limit per test 2 seconds memory ...

  10. windows 下python搭建环境

    一.python安装 1,首先访问https://www.python.org/downloads/windows/去下载python版本. 2. 选择3.5版本,installer文件类型(因为3. ...