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

#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. 并发回射服务器的最基本实现思路( fork )

    前言 一个服务器,通常会在一段时间内接收到多个请求.如果非要等到处理完一个请求再去处理下一个,势必会造成大部分用户的不满( 尤其当有某个请求需要占用大量时间时 ).如何解决这个问题?让处理这些用户请求 ...

  2. HDU5363:Key Set

    Problem Description soda has a set S with n integers {1,2,-,n}. A set is called key set if the sum o ...

  3. 程序猿的量化交易之路(27)--Cointrader之PriceData价格数据(14)

    转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ PriceData:价格数据.价格数据 ...

  4. springboot中tomcat找不到jsp页面【转载】

    这个原理还没搞明白,只知道是内嵌的tomcat找jsp时默认不读取resources目录,但是具体的默认读取的是哪个目录,打了一下午断点我也没找到.... 修改方式,添加配置修改tomcat的读取目录 ...

  5. LeastRecentlyUsed

    LeastRecentlyUsed Operating Systems http://www.cs.jhu.edu/~yairamir/cs418/os6/sld001.htm Cache repla ...

  6. chatty: uid=10549(u0_a549) com.exampleidentical 40 lines

    主要是因为你的app在短时间内输出太多次的日志,导致日志丢失 As soon as app considered 'chatty' by logcat (more than 5 lines per s ...

  7. 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 ...

  8. hdu-4991 Ordered Subsequence(dp+树状数组)

    题目链接: Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 32768/32768 K (Ja ...

  9. python中的linspace,meshgrid,concatenate函数

    linspace可以用来实现相同间隔的采样. numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) ...

  10. testing

    doctest_test.py def square(x): ''' 这里需要空格 >>> square(2) 4 啦啦 >>> square(3) 9 上不一行能 ...