1085: Water Problem

Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)
Total Submissions:1252   Accepted:132

[Submit][Status][Discuss]

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_(矩阵快速幂)的更多相关文章

  1. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  2. dutacm.club Water Problem(矩阵快速幂)

    Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Tot ...

  3. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  4. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  5. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  6. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  7. 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 ...

  8. 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 输 ...

  9. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

随机推荐

  1. 【BLE】CC2541之自己定义长短按键

    本篇博文最后改动时间:2017年01月06日,11:06. 一.简单介绍 本文以SimpleBLEPeripheral为例,介绍怎样将普通IO口(P12)自己定义为长短按键,实现按键3S以内松开为短按 ...

  2. python可变參数调用函数问题

    一直使用python实现一些想法,近期在使用python的过程中出现这样一个需求,定义了一个函数.第一个是普通參数.第二个是默认參数,后面还有可变參数,在最初学习python的时候,都知道非keywo ...

  3. 将mvvmlight 移植到 ios step1

    https://github.com/wangrenzhu/SimpleIoc-For-Objective-c simple ios for objective-c 版 基本实现 全部功能  完美实现 ...

  4. JavaScript基础 -- 常见DOM树操作

    1.创建并增加元素节点 <ul id="ul"> <li>1</li> <li>2</li> <li>3&l ...

  5. luogu 1083 借教室

    题目大意: 有一些教室 我们需要处理接下来n天的借教室信息 其中第i天学校有ri个教室可供租借 共有m份订单 每份订单用三个正整数描述 分别为dj sj tj 表示从第sj天到第tj天租借教室 每天需 ...

  6. 3-1 vue生存指南 - todolist实现-数据渲染

    由于Vue.js作者是中国人,会说汉语,所以国内生态会更好一点.Vue.js作者是尤雨溪,

  7. HTML文档中class的命名规则以及命名规范

    1.采用英文字母.数字以及“-”和“_”命名. 2.以小写字母开头,不能以数字和“-”.“_”开头. 3.命名形式:单字,连字符,下划线和驼峰. 4.使用有意义命名. 其中(3).(4)条规定主要是便 ...

  8. Akka源码分析-Cluster-ClusterClient

    ClusterClient可以与某个集群通信,而本身节点不必是集群的一部分.它只需要知道一个或多个节点的位置作为联系节点.它会跟ClusterReceptionist 建立连接,来跟集群中的特定节点发 ...

  9. 学习css盒子模型

    在这一周,我学习了css,在没有学习css之前,我一直都觉得布局很难,样式特别难调,但是学习了css盒子模型之后我就觉得欸,其实还挺简单的,下面就来看看我学习的css吧. CSS 盒子模型(Box M ...

  10. datatable-bootstrap 基本配置

    function doSearch() { if(dtable!=null){ dtable.fnClearTable(0); dtable.fnDraw(); // 重新加载数据 }else{ dt ...