• POJ


    题目已经告诉你斐波那契矩阵算法了...

    所以需要用到矩阵快速幂,看起来名字很diao但其实和普通快速幂基本一毛一样...

     1 #include <iostream>
     2 #include <string.h>
     3 #include <cstdio>
     4 #include <queue>
     5 #include <set>
     6 #include <stack>
     7 #include <math.h>
     8 #include <string>
     9 #include <algorithm>
    10 
    11 #define SIGMA_SIZE 26
    12 #define pii pair<int,int>
    13 #define lson rt<<1
    14 #define rson rt<<1|1
    15 #define lowbit(x) (x&-x)
    16 #define fode(i, a, b) for(int i=a; i>=b; i--)
    17 #define foe(i, a, b) for(int i=a; i<=b; i++)
    18 #define fod(i, a, b) for(int i=a; i>b; i--)
    19 #define fo(i, a, b) for(int i=a; i<b; i++)
    20 //#pragma warning ( disable : 4996 )
    21 
    22 using namespace std;
    23 typedef long long LL;
    24 inline LL LMax(LL a, LL b) { return a>b ? a : b; }
    25 inline LL LMin(LL a, LL b) { return a>b ? b : a; }
    26 inline LL lgcd(LL a, LL b) { return b == 0 ? a : lgcd(b, a%b); }
    27 inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; }  //a*b = gcd*lcm
    28 inline int Max(int a, int b) { return a>b ? a : b; }
    29 inline int Min(int a, int b) { return a>b ? b : a; }
    30 inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
    31 inline int lcm(int a, int b) { return a / gcd(a, b)*b; }  //a*b = gcd*lcm
    32 const LL INF = 0x3f3f3f3f3f3f3f3f;
    33 const LL lmod = 1e9+7;
    34 const int mod = 10000;
    35 const double eps = 1e-8;
    36 const int inf = 0x3f3f3f3f;
    37 const int maxk = 1e5+5;
    38 const int maxm = 510*510;
    39 const int maxn = 2;
    40 
    41 struct Matrix {
    42     int mm[maxn][maxn];
    43     Matrix() {}
    44     Matrix operator*(Matrix const &b) const
    45     {
    46         Matrix tmp; memset(tmp.mm, 0, sizeof(tmp.mm));
    47         fo(i, 0, maxn)
    48             fo(j, 0, maxn)
    49                 fo(k, 0, maxn)
    50                     tmp.mm[i][j] = (tmp.mm[i][j] + this->mm[i][k]*b.mm[k][j])%mod;
    51         return tmp;
    52     }
    53 };
    54 
    55 Matrix qpow(Matrix base, int b)
    56 {
    57     Matrix res; 
    58     memset(res.mm, 0, sizeof(res.mm));
    59     fo(i, 0, maxn) res.mm[i][i] = 1;
    60     while(b)
    61     {
    62         if (b&1) res = res*base;
    63         base = base*base;
    64         b >>= 1;
    65     }
    66     return res;
    67 }
    68 
    69 
    70 int main()
    71 {
    72 
    73     #ifndef ONLINE_JUDGE
    74         freopen("input.txt", "r", stdin);
    75     #endif
    76     
    77     int x;
    78     while(cin >> x)
    79     {
    80         if (x==-1) break;
    81         //if (x==0) cout << 0 << endl;
    82 
    83         Matrix tmp, ans; memset(tmp.mm, 0, sizeof(tmp.mm));
    84         tmp.mm[0][0] = tmp.mm[0][1] = tmp.mm[1][0] = 1;
    85         tmp.mm[1][1] = 0;
    86 
    87         ans = qpow(tmp, x);
    88         printf("%d
    ", ans.mm[0][1]);
    89     }
    90 
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    牛客小白月赛2 D 虚虚实实 【欧拉图】【连通图】
    牛客小白月赛2 A 数字方阵【随机】【找规律】
    牛客小白月赛1 J おみやげをまらいました 【MAP】
    牛客小白月赛1 I あなたの蛙が帰っています 【卡特兰数】
    欧拉函数
    乘法逆元
    扩展欧几里得
    快速乘法
    JPEG图像压缩出现资源不足问题的解决
    如何避免关键程序被意外关闭?
  • 原文地址:https://www.cnblogs.com/chaoswr/p/9780460.html
Copyright © 2020-2023  润新知