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

解题思路:令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. 08python之列表的常用方法

    列表list是python常用的数据类型,需要掌握以下常用方法: name_list = ['alex','tenglan','65brother'] 这个变量和之前的变量只存一个数字或字符串,这个列 ...

  2. resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced

    Maven在执行中报错: - Failure to transfer org.slf4j:slf4j-api:jar:1.7.24 from http://localhost:8081/nexus/c ...

  3. 基于麒麟座开发板2.0的MQTT实现例程

    链接--->https://sanwen8.cn/p/649shZ1.html OneNET现已全面适配标准MQTT协议,相信这一功能的增加会**便于开发者进行设备的接入. OneNET提供了M ...

  4. CSDN专栏收集

     Android集 1.Himi李华明的<Android游戏开发专栏>http://blog.csdn.net/column/details/androidgame.html2.老罗的&l ...

  5. LeetCode——Missing Number

    Description: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one t ...

  6. Kotlin——初级篇(一):最详细的环境搭建

    众所周知,Kotlin出来已经良久了.Kotlin有着众多优势,不管是用于Android开发中,还是Java开发,都能缩减很大的代码量,大大提高了工作效率.而小生本人也是才从忙碌的个工作中抽身出来,有 ...

  7. 常用的jq插件

    1.时间版翻转 FlipClock-master 2.整屏切换  fullpage 3.轮播图 swiper 4.滚动条样式 mCustomScrollbar 5.过滤和排序布局插件 Isotope. ...

  8. 【BZOJ4553】[Tjoi2016&Heoi2016]序列 cdq分治+树状数组

    [BZOJ4553][Tjoi2016&Heoi2016]序列 Description 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能 ...

  9. javaWeb中的文件上传下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  10. 【MySQL】为什么不要问我DB极限QPS/TPS

    为什么不要问我DB极限QPS/TPS 背景 相信很多开发都会有这个疑问,DB到底可以支撑多大的业务量,如何去评估?对于这个很专业的问题,DBA也没有办法直接告诉你,更多的都是靠经验提供一个看似靠谱的结 ...