Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 177761    Accepted Submission(s):
44124

Problem Description
A number sequence is defined as follows:

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

 
Input
The input consists of multiple test cases. Each test
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.
 
Output
For each test case, print the value of f(n) on a single
line.
 
Sample Input
1 1 3
1 2 10
0 0 0
 
Sample Output
2
5
 
Author
CHEN, Shunbao
 
Source
 
Recommend
 
JGShining   |   We have carefully selected several
similar problems for you:  1008 1004 1021 1019 1002 
 
 
这道题需要推一个类似于斐波那契矩阵的矩阵。
比较好推

 #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(矩阵)的更多相关文章

  1. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

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

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

  4. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  5. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

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

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

  8. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. 每个人都能实现的vue自定义指令

    前文 先来bb一堆废话哈哈.. 用vue做项目也有一年多了.除了用别人的插件之外.自己也没尝试去封装指令插件之类的东西来用. 刚好最近在项目中遇到一个问题.(快速点击按钮多次触发多次绑定的方法),于是 ...

  2. 紫书 例题8-18 UVa 1442 (扫描法)

    从左往右扫描一遍, 得从每个位置往右伸长不会碰到天花板的高度, 右往左一样, 取最小, 然后就是可以放"水"的高度了 #include<cstdio> #include ...

  3. Maven集成jetty插件

    本机环境 JDK 7 Maven 3.2 Jetty 8.1.9 Eclipse Luna pom.xml 配置 在你的 pom.xml 文件里加入 jetty 插件的描写叙述信息(查看Jetty很多 ...

  4. httpd: Could not reliably determine the server&#39;s fully qualified domain name

    [root@luozhonghua sbin]# service httpd start Starting httpd: httpd: apr_sockaddr_info_get() failed f ...

  5. 广东省知名P2P平台资料

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXNrYmFpNjY2ODg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  6. JavaSript之prototype属性

    近期在JavaSript进行Array操作的时候发现没有删除节点的方法.而我要实现的效果须要不断调用删除节点的方法.查找了相关资料发现能够利用prototype属性给Array添加删除节点的方法.而且 ...

  7. MySql基础总结(1)

    对于MySql已经是第二次看了,这次基本的目的是将MySql系统的归纳一边,之前学MySql是将笔记写在本子上,以后不想总带着本子,所以再花点时间把笔记归纳在博客中. 下午已将安装步骤具体的写了.如今 ...

  8. Import Example Dataset

    Overview The examples in this guide use the restaurants collection in the test database. The followi ...

  9. TurtleWorld Exercises

    1. Write a function called square that takes a parameter named t, which is a turtle. It should use t ...

  10. codeforces 540 C Ice Cave【BFS】

    题意:给出一个n*m的矩阵,“.”代表完整的冰,“X”代表破碎的冰,现在为了前进,需要掉下去一层,唯一的方法就是从破碎的冰上面掉下去 然后给出起点还有终点,问能否可达 即为到达终点的时候,终点必须是破 ...