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

InputThe 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. 
OutputFor 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 原谅博主不会Markdown

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); struct Matrix{
int a[][];
}; Matrix mul(Matrix a,Matrix b){
Matrix ans;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
ans.a[i][j]=;
for(int k=;k<=;k++){
ans.a[i][j]+=a.a[i][k]*b.a[k][j];
}
ans.a[i][j]%=mod;
}
}
return ans;
} Matrix q_pow(Matrix a,int b){
Matrix ans ;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
while (b){
if(b&){
ans=mul(ans,a);
}
b>>=;
a=mul(a,a);
}
return ans;
} int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); int A,B,n; while (scanf("%d%d%d",&A,&B,&n)!=EOF&&A&&B&&n){
Matrix exa;
if(n<=){printf("%d\n",);
continue;
}
exa.a[][]=A;
exa.a[][]=B;
exa.a[][]=;
exa.a[][]=; exa=q_pow(exa,n-);
printf("%d\n",(exa.a[][]+exa.a[][])%);
}
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 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

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

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

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

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

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

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

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

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

  9. hdu 5950 Recursive sequence 矩阵快速幂

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

  10. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

随机推荐

  1. SDUT-3364_欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在哥尼斯堡的一个公园里,有七座桥将普雷格 ...

  2. 基于GD库的php验证码类(支持中英文字体、背景、干扰点线、扭曲…….)

    转自:http://www.blhere.com/1168.html 12345678910111213141516171819202122232425262728293031323334353637 ...

  3. ural1297 后缀数组+RMQ

    RMQ即求区间(i,j)的最值.通过O(nlogn)处理,O(1)给出答案. RMQ主要是动态规划来做.dp[i][j]表示从i开始的长为2^j的区间最值. 那么可以得到dp[i][j]=max(dp ...

  4. 中断源记录 INT0 INT1

    中断源记录 INT0 INT1 用到一个单片机 使用的 P3.1 P3.3 作为唤醒口,后来发一 P3.1 和 P3.3 使用的同一个中断 INT1,这个尴尬了,只能两选 一. 查看规格书,还好 P3 ...

  5. HDOJ1016 Prime Ring Problem(DFS深层理解)

      Prime Ring Problem                                                                       时间限制: 200 ...

  6. shell去掉最后一个字符

    实测过第一种写法,可正常删除 sed 's/.$//' awk '{sub(/.$/,"")}1' awk '{printf $0"\b \n"}' ufile ...

  7. behavior planning——14.implement a cost function in C++

    n most situations, a single cost function will not be sufficient to produce complex vehicle behavior ...

  8. @省选模拟赛03/16 - T3@ 超级树

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 一棵 k-超级树(k-SuperTree) 可按如下方法得到:取 ...

  9. oracle强制索引失效

    如果两个或以上索引具有相同的等级,你可以强制命令ORACLE优化器使用其中的一个(通过它,检索出的记录数量少) . 举例: SELECT ENAME FROM EMP WHERE EMPNO = 79 ...

  10. codeforces1253F(图转换为树减少复杂度)

    题意: 给定一个无向图,其中1-k为充电桩,然后给定q个询问\(u_i, v_i\)(都是充电桩),然后问从其中一个充电桩到达另外一个充电桩需要最小的电池的容量. 每经过一条边都需要消耗一定的能量,到 ...