E题:Water Problem(快速幂模板)
解题思路:令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(快速幂模板)的更多相关文章
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
- 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 > ...
- POJ3070 矩阵快速幂模板
题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...
- 51nod1113(矩阵快速幂模板)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: #inc ...
- 89. a^b【快速幂模板】
a^b Description 求 aa 的 bb 次方对 pp 取模的值. 输入格式 三个整数 a,b,pa,b,p ,在同一行用空格隔开. 输出格式 输出一个整数,表示a^b mod p的值. 数 ...
- 矩阵快速幂模板(pascal)
洛谷P3390 题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格 ...
- CodeForces 450B (矩阵快速幂模板题+负数取模)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...
- hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)
Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...
- 求幂大法,矩阵快速幂,快速幂模板题--hdu4549
hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...
随机推荐
- 处理unsigned相减错误(BIGINT UNSIGNED value is out of range)
mysql 当两个字段想减时,如果其中一个或两个字段的类型的unsigned无签名类型,如果想减的值小于0则会报错(BIGINT UNSIGNED value is out of range) 测试: ...
- 详解google Chrome浏览器(理论篇)
注解:各位读者,经博客园工作人员反馈,hosts涉及违规问题,我暂时屏蔽了最新hosts,若已经获取最新hosts的朋友们,注意保密,不要外传.给大家带来麻烦,对此非常抱歉!!! 开篇概述 1.详解g ...
- 写了一个简单的CGI Server
之前看过一些开源程序的源码,也略微知道些Apache的CGI处理程序架构,于是用了一周时间,用C写了一个简单的CGI Server,代码算上头文件,一共1200行左右,难度中等偏上,小伙伴可以仔细看看 ...
- m2014-architecture-imgserver->配置lighttpd mod_mem_cache 模块做静态资源服务器
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freehat.blog.51cto.com/1239536/989171 一 l ...
- 【jdk源码学习】HashMap
package com.emsn.crazyjdk.java.util; /** * “人”类,重写了equals和hashcode方法...,以id来区分不同的人,你懂的... * * @autho ...
- 【文智背后的奥秘】系列篇——基于CRF的人名识别
版权声明:本文由文智原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/133 来源:腾云阁 https://www.qclou ...
- Unity3D笔记九 发送广播与消息、利用脚本控制游戏
一.发送广播与消息 游戏对象之间发送的广播与消息分为三种:第一种向子对象发送,将发送至该对象的同辈对象或者子孙对象中:第二种为给自己发送,发送至自己本身对象:第三种为向父对象发送,发送至该对象的同辈或 ...
- 阿里云搭建SVN服务器
1:安装svn apt-get install subversion 2. 开启svn服务器 svnserve -d 检查是否开启:ps aux | grep svnserve 若出现如下内容: wk ...
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
- 南京网络赛B-The writing on the wall
30.43% 2000ms 262144K Feeling hungry, a cute hamster decides to order some take-away food (like frie ...