POJ 3070 矩阵快速幂

  裸题,最简单fib的应用模板,算是新技能get 吧。

  其实和快速幂差不多了,只是矩阵代替的递推式。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 1005;
 6 struct node
 7 {
 8     int a[2][2];
 9     void init()
10     {
11         a[0][0] = a[1][0] = a[0][1] = 1;
12         a[1][1] = 0;
13     }
14 };
15 node mul(node a,node b)
16 {
17     node c;
18     for(int i = 0;i<2;++i)for(int j = 0;j<2;++j)
19     {
20         c.a[i][j] = 0;
21         for(int k = 0;k<2;++k)c.a[i][j] = (c.a[i][j]+a.a[i][k]*b.a[k][j])%10000;
22     }
23     return c;
24 }
25 node solve(int n)
26 {
27     node s,ret;
28     s.init();ret.init();
29     while(n)
30     {
31         if(n&1)ret = mul(ret,s);
32         s = mul(s,s);
33         n/=2;
34     }
35     return ret;
36 }
37 int main()
38 {
39     int n;
40     while(~scanf("%d",&n) && n!=-1)
41     {
42         if(!n){printf("0\n");continue;}
43         node ans = solve(n-1);
44         printf("%d\n",ans.a[1][0]%10000);
45     }
46     return 0;
47 }

  

时间: 2024-08-17 18:56:13

POJ 3070 矩阵快速幂的相关文章

POJ 3070 矩阵快速幂解决fib问题

矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdio> using namespace std; typedef long long ll; #define MOD 10000 ll a[7],b[7],a0[7],b0[7]; void pow_mod(ll n) { a0[1]=a0[2]=a0[3]=1,a0[4]=0; b0[1]=b0[4]=

Fibonacci (poj 3070 矩阵快速幂)

Language: Default Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10099   Accepted: 7211 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the

poj 3070 矩阵快速幂简单题

基本运用,基本是模板题. 求fi[n].       (1,1)    *( 1  ) ( 1,0)     (  0) #include<iostream> #include<cstring> using namespace std; struct juz { int bat[3][3]; int x,y; //行 列 }; juz mutp(juz a,juz b) { juz c; c.x=a.x;c.y=b.y; memset(c.bat,0,sizeof(c.bat));

poj 3070 矩阵快速幂模板

题意:求fibonacci数列第n项 1 #include "iostream" 2 #include "vector" 3 #include "cstring" 4 using namespace std; 5 6 typedef unsigned long int ULL; 7 typedef vector<ULL> vec; 8 typedef vector<vec> mat; 9 const ULL P=10000

POJ 3070-Fibonacci(矩阵快速幂求斐波那契数列)

Fibonacci Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3070 Appoint description:  System Crawler  (2015-02-28) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 +

poj 3233(矩阵快速幂)

题目链接:http://poj.org/problem?id=3233: 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以很显然是矩阵快速幂,但有一点,本题k的值非常大,所以要用二分求和来减少运行时间. 代码: #include <set> #include <map> #include <stack> #include <queue> #include <math.h> #include <vector> #in

poj 3734 矩阵快速幂+YY

题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中,红.绿方块数量都是偶数的方案数 b[i]=1-i方块中,红.绿方块数量一个是偶数一个是奇数的方案数(红even绿odd 或 红odd绿even) c[i]=1-i方块中,红.绿方块数量都是奇数的方案数 可以得出递推公式: a[i+1]=2*a[i]+b[i] b[i+1]=2*a[i]+2*b[i

Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板

题目链接  请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m <

poj 3233 矩阵快速幂+YY

题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角那块就可以得到要求的解S 我们取这一块,再减去一个单位矩阵1即可. 1 #include "iostream" 2 #include "vector" 3 #include "cstring" 4 #include "cstdio"