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

题解:把系数矩阵的数换下即可

代码:

#include<stdio.h>
#include<string.h>
#define MAX 10
typedef long long ll; ll p,q,MOD=7;
struct mat{
ll a[MAX][MAX];
}; mat operator *(mat x,mat y) //重载*运算
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
for(int k=1;k<=2;k++){
ans.a[i][j]+=x.a[i][k]*y.a[k][j];
ans.a[i][j]%=MOD;
}
}
}
return ans;
}
mat qsortMod(mat a,ll n) //矩阵快速幂
{
mat t;
t.a[1][1]=p;t.a[1][2]=q; //变式的系数
t.a[2][1]=1;t.a[2][2]=0;
while(n){
if(n&1) a=t*a; //矩阵乘法不满足交换律,t在前
n>>=1;
t=t*t;
}
return a;
}
int main()
{
ll n;
while(scanf("%lld%lld%lld",&p,&q,&n)!=EOF)
{
if(p==0&&q==0&&n==0)
break;
if(n==1) printf("1\n");
else if(n==2) printf("1\n");
else{
mat a;
a.a[1][1]=1;a.a[1][2]=0;
a.a[2][1]=1;a.a[2][2]=0;
a=qsortMod(a,n-2);
printf("%lld\n",a.a[1][1]);
}
}
return 0;
}

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 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  4. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  5. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  6. Yet another Number Sequence 矩阵快速幂

    Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...

  7. SDUT1607:Number Sequence(矩阵快速幂)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...

  8. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  9. CodeForces 392C Yet Another Number Sequence 矩阵快速幂

    题意: \(F_n\)为斐波那契数列,\(F_1=1,F_2=2\). 给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\). 求\(A_1+A_2+ \cdots + A_n\). ...

随机推荐

  1. IntelliJIDEA的安装、配置与使用

    引言: IDEA,全称 IntelliJ IDEA,是 Java 语言的集成开发环境,IDEA 在业界被公认为是 最好的 java 开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE 支持 ...

  2. Android SP的具体内容

    过了这么久了,看看自己的园龄都17天了,一直在总结,从未缺席,我还是很开心的,踏踏实实的完成自己能学到的. 今天学习SP SP:全称SharedPreferences,别问我为啥知道,因为打了好多遍了 ...

  3. 趣讲 PowerJob 超强大的调度层,开始表演真正的技术了

    本文适合有 Java 基础知识的人群 作者:HelloGitHub-Salieri HelloGitHub 推出的<讲解开源项目>系列. 写在前面的碎碎念:终于到了万众期待的调度层原理了. ...

  4. 浅析Facebook LibraBFT与比原链Bystack BBFT共识

    如果说什么是区块链的灵魂,那一定是共识机制. 它是区块链的根基.无论公链或是联盟链,共识机制都从基础上限制了区块链的交易处理能力和扩展性. 2019年6月18日,Facebook 发布了自己 Libr ...

  5. MnasNet:Mobile Neural Architecture Search(MNAS)

  6. java基础之字符串

    以下内容摘自<java编程思想>第十三章. 1. 不可变 String String 对象是不可变对象,String 类中每一个看起来会修改 String 值的方法,实际上都是创建了一个全 ...

  7. 数据结构C++实现邻接矩阵存储图

    定义邻接矩阵存储的图类.[实验要求] 1. 创建一个邻接矩阵存储的图: 2. 返回图中指定边的权值: 3. 查找图中某顶点的第一个邻接顶点.某顶点关于另一个顶点的下一个邻接顶点序号: 4. 图的深度优 ...

  8. .net Core使用sql语句实现批量修改数据状态

    上图为查出的所有满足条件的数据,要选中若干条数据将其状态设置为作废 一共选中6条数据,当点击确认后修改数据状态. 前端代码 1.安装NuGet包 [Abp.Dapper]于EFCore中, 2.创建文 ...

  9. DataGrid添加进度条列

    DataGridColumn类型的继承树 DataGridColumn的派生类: 一般情况下DataGridBoundColumn和DataGridComboBoxColumn足以满足多数列的样式,如 ...

  10. ipvsadm服务报错/bin/bash: /etc/sysconfig/ipvsadm: No such file or directory

    问题: 在执行重启ipvsadm服务时报错: 提示没有找到/etc/sysconfig/ipvsadm 解决: [root@lvs1 ~]# ipvsadm --save > /etc/sysc ...