Number Sequence(快速幂矩阵)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005

Number Sequence

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

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

ZJCPC2004

题意:简单的快速幂矩阵,加一个f(n-1) = f(n-1)即可

代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 struct Mtr{
 8     int a , b , c , d ;
 9     Mtr operator * (const Mtr m) const
10     {
11         Mtr res ;
12         res.a = (a*m.a%7 + b*m.c%7)%7;
13         res.b = (a*m.b%7+b*m.d%7)%7;
14         res.c = (c*m.a%7+d*m.c%7)%7;
15         res.d = (c*m.b%7+d*m.d%7)%7;
16         return res;
17     }
18 };
19 int cal(Mtr a , int n)
20 {
21     Mtr c;
22     c.a = c.d = 1;
23     c.b = c.c = 0;
24     while(n>0)
25     {
26         if(n&1)
27             c = c*a;
28         a = (a*a);
29         n/=2;
30     }
31     return (c.a+c.b)%7;
32 }
33 int main()
34 {
35     int a , b , n;
36     while(~scanf("%d%d%d",&a,&b,&n)&&(a!=0||b!=0||n!=0))
37     {
38         Mtr tm;
39         tm.a = a;
40         tm.b = b;
41         tm.c = 1;
42         tm.d = 0;
43         int tt = cal(tm,n-2);
44         printf("%d\n",tt);
45     }
46     return 0;
47 }
时间: 2024-10-27 19:31:51

Number Sequence(快速幂矩阵)的相关文章

hdu 4965 矩阵快速幂 矩阵相乘性质

Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a gir

hdu_2604Queuing(快速幂矩阵)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4428    Accepted Submission(s): 1961 Problem Description Queues and Priority Queues a

[POJ 3150] Cellular Automaton (矩阵快速幂 + 矩阵乘法优化)

Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 1227 Case Time Limit: 2000MS Description A cellular automaton is a collection of cells on a grid of specified shape that evolves through a number of dis

HDU 3306 Another kind of Fibonacci(快速幂矩阵)

题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MOD 10007 #defin

菲波那契数列的快速幂矩阵求法

时间:2014.05.15 地点:基地二楼 ----------------------------------------------------------------------- 一.背景 著名的斐波那契数列为一个这样的序列:0 1 1 2 3 5 8 13 21 34......简单的递推公式如下: F(0)=0,F(1)=1,当n>=1时,F(n)=F(n-1)+F(n-2) 显然,我们用直接的按公式递归的算法去计算该数列的第n项效率并不高,因为这样每次递归调用我们只是将规规模缩小了

HDU 4686 Arc of Dream(快速幂矩阵)

题目链接 再水一发,构造啊,初始化啊...wa很多次啊.. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MOD 1000000007 #define

斐波那契优化 快速幂+矩阵乘法

题目:你能求得第n个斐波那契数吗?0<n<maxlongint 由于结果太大,输出的结果mod32768 思路:一般的求斐波那契数列的方法有递归,动归,或者用滚动优化,但是空间复杂或者时间复杂度都太高了,现在有一种用矩阵加快速幂的优化算法,可以让时间复杂度维持在logn. 具体的 初始化一个2×2的矩阵,初始值为{1,0,0,1} 则分别代表{a2,a1,a1,a0},把此矩阵平方后得到{2,1,1,0}分别代表{a3,a2,a2,a1}如此下去,便可以得到规律,其实这个算法主要就是优化在快速

HDU 4965 Fast Matrix Caculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 )

HDU 4965 Fast Matrix Calculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 1001 #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 6 typedef long lo

递推求值【快速幂矩阵】

递推求值 描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f(n)的值,由于f(n)的值可能过大,求出f(n)对1000007取模后的值. 注意:-1对3取模后等于2   输入 第一行是一个整数T,表示测试数据的组数(T<=10000)随后每行有六个整数,分别表示f(1),f(2),a,b,c,n的值.其中0<=f(1),f(2)<100,-100<=a,b,c<=100,1<=n<=10000000