dutacm.club_1085_Water Problem_(矩阵快速幂)
1085: Water Problem
Total Submissions:1252 Accepted:132
Description
函数 f:Z+→Z。已知 f(1),f(2) 的值,且对于任意 x>1x>1,有 f(x+1)=f(x)+f(x−1)+sin(πx/2)。
求 f(n)f(n) 的值。
Input
多组数据。(数据组数 T≤100)
每组数据包含 3 个不超过 10^9 的正整数,分别代表 f(1),f(2)和 n 的值。
Output
输出 f(n)mod(10^9+7)。每组输出末尾有换行符。
Sample Input
1 2 3
1 2 5
Sample Output
3
7 题意很清楚。题目出处:http://dutacm.club:7217/codesheaven/problem.php?id=1085
思路:也是看的题解,之前就是最后的sin解决不了。使用此处sin函数的周期为4,那么每半个周期可以将sin抵消掉。
f(x+1)=f(x)+f(x-1)+sin(πx/2) 1式
f(x-1)=f(x-2)+f(x-3)+sin(π(x-2)/2) 2式
将2式带入1式,得f(x+1)=f(x)+f(x-2)+f(x-3),然后转移矩阵很容易写出。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
#define MOD 1000000007
#define LL long long int Sin[]= {,,,-};
struct Matrix
{
int row,col;
LL matr[][];
Matrix() {}
Matrix(int r,int c,int num=)
{
row=r;
col=c;
for(int i=; i<=r; i++)
for(int j=; j<=c; j++)
matr[i][j]=num;
}
}; Matrix matr_multi(Matrix m1,Matrix m2) //矩阵乘法
{
Matrix m3(m1.row,m2.col,);
for(int i=; i<=m1.row; i++)
for(int j=; j<=m2.col; j++)
for(int k=; k<=m1.col; k++)
m3.matr[i][j]=(m3.matr[i][j]+m1.matr[i][k]*m2.matr[k][j])%MOD;
return m3;
} void matr_givevalue(Matrix& a,Matrix b)
{
a.row=b.row;
a.col=b.col;
for(int i=; i<=a.row; i++)
for(int j=; j<=a.col; j++)
a.matr[i][j]=b.matr[i][j];
} Matrix matr_pow(Matrix m1,int k) //矩阵快速幂
{
Matrix m2;
matr_givevalue(m2,m1);
k--;
while(k>)
{
if(k&)
m2=matr_multi(m2,m1);
m1=matr_multi(m1,m1);
k>>=;
}
return m2;
} LL PowMod(LL n,int k) //常规快速幂
{
LL res=;
while(k>)
{
if(k&)
res=(res*n)%MOD;
n=(n*n)%MOD;
k>>=;
}
return res;
} void matr_output(Matrix m)
{
for(int i=; i<=m.row; i++)
{
for(int j=; j<=m.col; j++)
cout<<m.matr[i][j]<<" ";
cout<<endl;
}
}
int main()
{
LL f1,f2,n;
while(scanf("%lld%lld%lld",&f1,&f2,&n)!=EOF)
{
Matrix m1(,);
m1.matr[][]=f1;
m1.matr[][]=f2;
m1.matr[][]=(f1+f2+Sin[])%MOD;
m1.matr[][]=(m1.matr[][]+m1.matr[][]+Sin[])%MOD;
Matrix m2(,,);
m2.matr[][]=m2.matr[][]=m2.matr[][]=m2.matr[][]=;
m2.matr[][]=m2.matr[][]=;
if(n>)
{
m2=matr_pow(m2,n-);
m1=matr_multi(m1,m2);
printf("%lld\n",m1.matr[][]);
}
else
{
printf("%lld\n",m1.matr[][-n]);
}
} return ;
}
dutacm.club_1085_Water Problem_(矩阵快速幂)的更多相关文章
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- dutacm.club Water Problem(矩阵快速幂)
Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Others)Tot ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
- hdu2604(递推,矩阵快速幂)
题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...
随机推荐
- spring test---restful与文件上传
spring提供了大量经常使用的功能測试,如文件上传.restful风格url訪问.以下介绍主要介绍下test中经常使用功能的使用方法: 首先能够静态导入类.方便在測试类中使用,导入的类有 impor ...
- ≪统计学习精要(The Elements of Statistical Learning)≫课堂笔记(三)
照例文章第一段跑题,先附上个段子(转载的哦~): I hate CS people. They don't know linear algebra but want to teach projecti ...
- Unity3D & C# 设计模式--23
Unity3D & C#Design Patterns 23 design patterns. Creational Patterns 1. Abstract Factory抽象工厂 创 ...
- 分析Linux内核的启动过程
第一章 环境 Ubuntu 14.10 Linux Kernel 3.18.6 第二章 代码及调试过程 环境搭建与内核准备: cd ~/LinuxKernel/ wget https://www.ke ...
- 服务器无法处理请求。 ---> 未将对象引用设置到对象的实例
服务器无法处理请求. ---> 未将对象引用设置到对象的实例. 简短说下我遇到的问题.webservice部署到服务器上后,访问方法报上面的错误,最终原因为改方法需要操作文件夹,加上了相应的权限 ...
- 容器HashSet原理(学习)
一.概述 使用HashMap存储,非线程安全: 二.实现 HashSet 底层使用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,相关 HashSet 的操作,基本上都是直接调 ...
- Bootstrap Tooltip源码分析
/* ======================================================================== * Bootstrap: tooltip.js ...
- YTU 2633: P3 数钱是件愉快的事
2633: P3 数钱是件愉快的事 时间限制: 1 Sec 内存限制: 128 MB 提交: 387 解决: 215 题目描述 超市收银员的钱盒里,各种钞票总是按照面额分类整理,这样做可以提高效率 ...
- 【POJ 2983】 Is the information reliable?
[题目链接] 点击打开链接 [算法] 差分约束系统,SPFA判负环 [代码] #include <algorithm> #include <bitset> #include & ...
- LaTex简历排版
为了写好简历,倒腾了好几天的Latex,主要是因为中文支持问题还有morderncv的更新问题... 1. http://www.ctex.org/CTeXDownload/ 建议下载v2.9.2. ...