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. 32-关键字:abstract

    abstract: 抽象的 1.可以用来修饰:类.方法 2.具体的:abstract修饰类:抽象类 * > 此类不能实例化 * > 抽象类中一定有构造器,便于子类实例化时调用(涉及:子类对 ...

  2. 加密模块hashlib模块(* *)

    用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 越往后加密算法越复杂,这些算法都是不可逆的,MD5 算法 ...

  3. CSS网页排版

    自印刷出版物诞生以来,排版就一直是平面设计的基础. 同样,排版在网页设计中也扮演着重要角色. 1.CSS的基本排版技术 1.1 文本颜色 对应网页而言,文本颜色也许是最基本的样式之一. 默认情况下,浏 ...

  4. MySQL“被动”性能优化汇总!

    年少不知优化苦,遇坑方知优化难. --村口王大爷 本文内容导图如下: 我之前有很多文章都在讲性能优化的问题,比如下面这些: <switch 的性能提升了 3 倍,我只用了这一招!> < ...

  5. 你知道MySQL是如何处理千万级数据的吗?

    mysql 分表思路 一张一亿的订单表,可以分成五张表,这样每张表就只有两千万数据,分担了原来一张表的压力,分表需要根据某个条件进行分,这里可以根据地区来分表,需要一个中间件来控制到底是去哪张表去找到 ...

  6. C#LeetCode刷题之#559-N叉树的最大深度​​​​​​​(Maximum Depth of N-ary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4088 访问. 给定一个 N 叉树,找到其最大深度. 最大深度是指 ...

  7. Vue 给页面加水印指令(directive)

    页面需要水印 import Vue from 'vue' /** * watermark 指令 * 解决: 给页面生成水印 * 基本原理:给选择器添加背景图片 * 用法:v-watermark=&qu ...

  8. Homekit_二路继电器

    介绍一款二路继电器,使用Homekit进行控制,有兴趣的可以去以下链接看看: https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-11265006 ...

  9. 《Windows程序设计(第5版 珍藏版)》配书光盘

    https://pan.baidu.com/s/1ro72qQja_xTbf-Ik8b06Ng

  10. 同事不太懂负载均衡,我直接把阿里架构师的这份Nginx笔记甩给他

    Nginx功能强大,架构复杂,学习.维护和开发的门槛较高. 本份笔记深入最新的Nginx源码,详细剖析了模块体系.动态插件.功能框架.进程模型.事件驱动.线程池.TCP/UDP/HTTP 处理等Ngi ...