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父子组件通信传值

    父组件 -> 子组件 通过props来进行通信 父组件代码: <Children :dataName = "dataContent" /> //dataName: ...

  2. HDOJ1084 What Is Your Grade?

    What Is Your Grade? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. 【JS】怎样用原生JS实现jQuery的ready方法

    Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,只是与window.onload方法还是有差别的. 总的来说,window. ...

  4. 由动态库文件dll生成lib库文件

    本文基于OpenBlas的编译和安装.来说明怎样从一个dll文件生成lib库文件. 參考OpenBlas的说明"Howto generate import library for MingW ...

  5. Windows下VS2013+Caffe无GPU配置

    Windows版本的caffe工具包下载地址: 点击打开链接 1. 将下载的caffe-master.zip解压到 D:\Software\Caffe 文件夹下,把 D:\Software\Caffe ...

  6. 32.智能指针auto_ptr

    #include <iostream> #include <memory> #include <string> #include <vector> us ...

  7. POJ 3661 DP

    题意: 思路: i表示到了i,j表示疲劳度为j f[i][j]表示能跑的最大距离 f[i][j]=f[i-1][j-1]+a[i] if(i-j>=0)f[i][0]=max(f[i][0],f ...

  8. $.widget 编写jQueryUI插件(widget)

    转自:MainTao: 编写jQueryUI插件(widget) 使用jQueryUI的widget来写插件,相比于基本的jquery插件有一些好处: * 方便实现继承,代码重用 * 默认是单例 * ...

  9. UVa 10305 Ordering Tasks【拓扑排序】

    题意:给出n件事情,m个二元组关系,求它们的拓扑序列 用的队列来做 #include<iostream> #include<cstdio> #include<cstrin ...

  10. Debian9.5 系统配置NFS配置说明

    NFS是Network File System的缩写,即网络文件系统.它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录.NFS客户端可以通过挂载(mount)的方式将NFS ...