题目大意:原题链接  题解链接

解题思路:令x=x-1代入原等式得到新的等式,两式相加,将sin()部分抵消掉,得到只含有f(x)的状态转移方程f(x+1)=f(x)+f(x-2)+f(x-3),然后用矩阵快速幂即可

#include<cstdio>
#include<cstring>
typedef long long ll;
const ll mod=1e9+;
long long f[];
int temp[]={,,,-};
struct Mat
{
ll mat[][];
}res; Mat Mult(Mat a,Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
return c;
}
Mat QMult(Mat a,ll b)
{
Mat t;
for(int i=;i<;i++){
for(int j=;j<;j++){
t.mat[i][j]=i==j;
}
}
while(b){
if(b&)
t=Mult(t,a);//注意方向,t在前,a在后
a=Mult(a,a);
b>>=;
}
return t;
} int main()
{
int a,b,n;
while(scanf("%d%d%d",&a,&b,&n)!=EOF){
f[]=a,f[]=b;
for(int i=;i<=;i++)
f[i]=f[i-]+f[i-]+temp[(i-)%];
if(n<=){
printf("%d\n",f[n]);
continue;
}
res.mat[][]=res.mat[][]=res.mat[][]=;
res.mat[][]=res.mat[][]=res.mat[][]=;
Mat ans=QMult(res,n-);
int anss=(ans.mat[][]*f[]%mod)+(ans.mat[][]*f[]%mod);
anss=anss+(ans.mat[][]*f[]%mod)+(ans.mat[][]*f[]%mod);
printf("%d\n",anss%mod);
}
}
#include<cstdio>
#include<cstring>
typedef long long ll;
const ll mod=1e9+;
long long f[];
int temp[]={,,,-};
struct Mat
{
ll mat[][];
}res; Mat Mult(Mat a,Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
return c;
}
Mat QMult(Mat a,ll b)
{
Mat t;
memset(t.mat,,sizeof(t.mat));
t.mat[][]=t.mat[][]=t.mat[][]=;
t.mat[][]=t.mat[][]=t.mat[][]=;
while(b){
if(b&)
a=Mult(t,a);
t=Mult(t,t);
b>>=;
}
return a;
} int main()
{
int a,b,n;
while(scanf("%d%d%d",&a,&b,&n)!=EOF){
f[]=a,f[]=b;
for(int i=;i<=;i++)
f[i]=f[i-]+f[i-]+temp[(i-)%];
if(n<=){
printf("%d\n",f[n]);
continue;
}
res.mat[][]=f[],res.mat[][]=f[];
res.mat[][]=f[],res.mat[][]=f[];
Mat ans=QMult(res,n-);
printf("%d\n",ans.mat[][]%mod);
}
}

E题:Water Problem(快速幂模板)的更多相关文章

  1. luoguP3390(矩阵快速幂模板题)

    链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...

  2. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  3. POJ3070 矩阵快速幂模板

    题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...

  4. 51nod1113(矩阵快速幂模板)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: #inc ...

  5. 89. a^b【快速幂模板】

    a^b Description 求 aa 的 bb 次方对 pp 取模的值. 输入格式 三个整数 a,b,pa,b,p ,在同一行用空格隔开. 输出格式 输出一个整数,表示a^b mod p的值. 数 ...

  6. 矩阵快速幂模板(pascal)

    洛谷P3390 题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格 ...

  7. CodeForces 450B (矩阵快速幂模板题+负数取模)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...

  8. hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)

    Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...

  9. 求幂大法,矩阵快速幂,快速幂模板题--hdu4549

    hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...

随机推荐

  1. 简单易懂的snmpd.conf配置文件说明

    转自http://blog.chinaunix.net/u2/61187/showart_689604.html 用 snmp+mrtg 可以很好的实现对局域网内服务器状态的监控.      现在就以 ...

  2. VS2015编译OpenSSL1.0.2源码

    更多详细信息http://blog.csdn.net/YAOJINGKAO/article/details/53041165?locationNum=10&fps=1 1.下载安装编译必须的A ...

  3. css纯字母或者字母换行显示

    white-space:normal; word-break:break-all;

  4. PyQt4关闭窗口

    一个显而易见的关闭窗口的方式是但集标题兰有上角的X标记.接下来的示例展示如何用代码来关闭程序,并简要介绍Qt的信号和槽机制. 下面是QPushButton的构造函数,我们将会在下面的示例中使用它. Q ...

  5. JAXB简单样例

    参考网页:http://www.mkyong.com/java/jaxb-hello-world-example/JAXB完整教程:https://jaxb.java.net/tutorial/1.J ...

  6. iOS 自动布局 Autolayout 优先级的使用

    一.约束的优先级 0.屏幕适配 发展历程 代码计算frame -> autoreszing(父控件和子控件的关系) -> autolayout(任何控件都可以产生关系) -> siz ...

  7. windows 上驱动阻止关机重启操作

    Windows 上关机重启有很多相关的操作 HOOK 一个点搞不定  具体需要以下 4 处来布控 SSDT HOOK NtInitiatePowerAction 函数 ,直接返回失败废掉这个函数 SS ...

  8. springboot---->springboot的使用(一)

    这里我们记录一下springboot的使用,第一次创建环境. springboot的使用 项目结构如下: 一.我们使用maven去构建springboot的依赖.其中我们使用的pom.xml文件内容如 ...

  9. sencha touch 入门系列 (八)sencha touch类系统讲解(下)

    接着上一讲,我们通过一组代码来讲解一下st的类的一些属性: Ext.define("MyConfig",{ config:{ website:"http://127.0. ...

  10. click() bind() live() delegate()区别

    click(),bind(),live()都是执行事件时使用的方法 1.click()单击事件方法: $("a").click(function() { alert("h ...