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

#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. 面向对象基础——String类

    String类的两种实例化方法  A:直接赋值 public class StringDemo01{ public static void main(String args[]){ String na ...

  2. Eclipse-----Eclipse断点调试

  3. 2017NOIP游记 (格式有点炸)

    NOIP游记 作者:一只小蒟蒻 时间可真快呀!还记得我第一次接触信息竞赛时,hello world都要调好久,不知不觉就考完了2017noip,自我感觉良好(虽然还是有很多不足). 这两个月的闭关,让 ...

  4. Linux环境下安装Tomcat

    一.准备安装的tar包 1.将安装包放在服务器上:apache-tomcat-7.0.81.tar.gz 2.将安装包解压:tar -zxvf apache-tomcat-7.0.81.tar.gz ...

  5. PO 审批及生成xml文件

    *********************************************************************** * Report : YTST_RAINY_MM2 * ...

  6. 【WordSearch】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. html5--4-4 audio元素/格式的转换

    html5--4-4 audio元素/格式的转换 学习要点 掌握audio元素的用法 视频/音频文件的格式转换 当前,audio 元素支持三种音频格式: Ogg 免费, 支持的浏览器:Firefox. ...

  8. hdu-4990 Reading comprehension(快速幂+乘法逆元)

    题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  9. (QACNN)自然语言处理:智能问答 IBM 保险QA QACNN 实现笔记

    follow: https://github.com/white127/insuranceQA-cnn-lstm http://www.52nlp.cn/qa%E9%97%AE%E7%AD%94%E7 ...

  10. DNS多出口分析

    DNS多出口分问题现象:当dns解析出的ip非域名的本地覆盖组,则怀疑是DNS多出口或者DNS劫持.接下来判断该ip是否为网宿ip,如果不是,则是劫持问题,走劫持流程进行反馈.如果是网宿ip,则用以下 ...