HDU 1005 Number Sequence(矩阵)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 177761 Accepted Submission(s):
44124
f(1) =
1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and
n, you are to calculate the value of f(n).
case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1
<= n <= 100,000,000). Three zeros signal the end of input and this test
case is not to be processed.
line.
1 2 10
0 0 0
5
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXN=;
inline void read(int &n){char c='+';bool flag=;n=;
while(c<''||c>'') c=='-'?flag=,c=getchar():c=getchar();
while(c>=''&&c<='') n=n*+c-,c=getchar();flag==?n=-n:n=n;}
struct matrix
{
int m[][];matrix(){memset(m,,sizeof(m));}
};
matrix ma;
int limit=;
const int mod=;
matrix mul(matrix a,matrix b)
{
matrix c;
for(int k=;k<limit;k++)
for(int i=;i<limit;i++)
for(int j=;j<limit;j++)
c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j]))%mod;
return c;
}
matrix fast_martix_pow(matrix ma,int p)
{
matrix bg;
bg.m[][]=;bg.m[][]=;
bg.m[][]=;bg.m[][]=;
/*for(int i=0;i<limit;i++)
{
for(int j=0;j<limit;j++)
cout<<bg.m[i][j]<<" ";
cout<<endl;
}*/ while(p)
{
if(p&) bg=mul(bg,ma);
ma=mul(ma,ma);
p>>=;
}
return bg;
}
int main()
{
int a,b,n;
while(scanf("%d%d%d",&a,&b,&n)&&(a!=&&b!=&&n!=))
{
ma.m[][]=a;ma.m[][]=b;
ma.m[][]=;ma.m[][]=;
if(n<)
{
printf("1\n");
continue;
}
matrix ans=fast_martix_pow(ma,n-);
printf("%d\n",(ans.m[][]+ans.m[][])%mod);
}
return ;
}
HDU 1005 Number Sequence(矩阵)的更多相关文章
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1005 Number Sequence(数论)
HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...
- HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU - 1005 Number Sequence (矩阵快速幂)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...
- HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- PNG文件结构分析
http://blog.163.com/iwait2012@126/blog/static/16947232820124411174877/ PNG文件结构分析 对于一个PNG文件来说,其文件头总是由 ...
- ACM:动态规划,01背包问题
题目: 有n件物品和一个容量为C的背包.(每种物品均仅仅有一件)第i件物品的体积是v[i],重量是w[i].选一些物品装到这个背包中,使得背包内物品在整体积不超过C的前提下重量尽量大. 解法:两种思路 ...
- An existing connection was forcibly closed by the remote host
StackOverflow https://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-con ...
- Innosetup
卸载的同时删除日志,卸载的时候判断程序是否正在运行,regsvr32 1.卸载程序的时候如何判断程序是否正在运行 http://bbs.csdn.net/topics/370097914 2.强制删除 ...
- BP神经网络模型及梯度下降法
BP(Back Propagation)网络是1985年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一. B ...
- 5. MongoDB基本操作语句
转自:http://blog.51cto.com/shanqiangwu/1653577 #MongoDB中有三元素:数据库,集合,文档,其中“集合”就是对应关系数据库中的“表”,“文档”对应“行”. ...
- Case study: word play
For the exercises in this chapter we need a list of English words. There are lots of word lists avai ...
- OpenGL的前世和今生
这并不是一个恰当的题目,因为我主要想说的是OpenGL的今生,基于OpenGL3.x一种更现代化的方式.但是把前世和今生放在一起在语言上更加连贯,而且适当的了解过去,会帮助理解现在的OpenGL,以一 ...
- 实时监控Cat之旅~配置Cat集群需要注意的问题
在配置cat集群时,有一些设置是我们应该注意的,从它的部署文档中我们可以看到相关信息,但说的还不够明确和重要,大叔今天总结一下Cat集群配置的注意事项 1 服务端datasources.xml用来设置 ...
- SQLServer中同义词Synonym的用法
以前一直认为SqlServer中的同义词(Synonym)没有什么用处,所以也一直没有去查它的语法格式.今天碰到一个问题,用Synonym来解决再好不过了.问题是这样子的,我的系统中用到了多个数据库, ...