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

解题思路:令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. J2EE开发推荐工具

  2. POJ 1243 One Person

    题意: 猜数字, 给定 G, L, G 表示可以猜的次数, 每猜一次, G减一, 假如猜的 number 大于 target, L 还需减一, 当 L == -1 或者 G==0 时, 若还没猜中, ...

  3. swift--使用 is 和 as 操作符来实现类型检查和转换 / AnyObject与Any的区别

    声明几个类: //动物类 class Animal{ } //陆地动物类 class terricole: Animal { } //海洋动物类 class SeaAnimals: Animal { ...

  4. python2.0_s12_day9之day8遗留知识(queue队列&生产者消费者模型)

    4.线程 1.语法 2.join 3.线程锁之Lock\Rlock\信号量 4.将线程变为守护进程 5.Event事件 * 6.queue队列 * 7.生产者消费者模型 4.6 queue队列 que ...

  5. list的下标【python】

    转自:http://www.cnblogs.com/dyllove98/archive/2013/07/20/3202785.html list的下表从零开始,和C语言挺类似的,但是增加了负下标的使用 ...

  6. linux系统socket通信编程2

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  7. Linux性能监控——CPU,Memory,IO,Network

    版权声明:本文由刘爽原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/107 来源:腾云阁 https://www.qclou ...

  8. Excel 2010 对号叉号怎么打出来

    按小键盘数字键:Alt+41420  对号 按小键盘数字键:Alt+41642  叉号 http://jingyan.baidu.com/article/fdbd4277c228cdb89e3f482 ...

  9. 【Android N 7】使用量统计usagestats

    Android N 7.1.1 高通 1. /data/system/usagestats/0 2. 每天使用量统计 /data/system/usagestats/0/daily 查看数值: cat ...

  10. SRC常见WEB漏洞系列之HTTP-HOST头攻击

    一.背景: web程序需要知道网站的域名比较麻烦,需要使用HTTP的 host头字段: <?php _SERVER["HTTP_HOST"] ?> @app.route ...