【51NOD1242】斐波那契数列的第N项(矩阵快速幂)

来源:互联网 发布:自动变速箱编程 编辑:程序博客网 时间:2024/06/01 01:33

记录一个菜逼的成长。。

题目链接
ps:只是记下模板。

这题有个坑,用多组输入就TLE.

//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdlib>#include <vector>#include <set>#include <map>#include <queue>#include <stack>#include <list>#include <deque>#include <cctype>#include <bitset>#include <cmath>#include <cassert>using namespace std;#define ALL(v) (v).begin(),(v).end()#define cl(a,b) memset(a,b,sizeof(a))#define bp __builtin_popcount#define pb push_back#define mp make_pair#define fin freopen("D://in.txt","r",stdin)#define fout freopen("D://out.txt","w",stdout)#define lson t<<1,l,mid#define rson t<<1|1,mid+1,r#define seglen (node[t].r-node[t].l+1)#define pi 3.1415926#define exp  2.718281828459#define lowbit(x) (x)&(-x)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;typedef pair<LL,LL> PLL;typedef vector<PII> VPII;const int INF = 0x3f3f3f3f;//const int MOD = 1e9 + 7;template <typename T>inline void read(T &x){    T ans=0;    char last=' ',ch=getchar();    while(ch<'0' || ch>'9')last=ch,ch=getchar();    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();    if(last=='-')ans=-ans;    x = ans;}const int MOD = 1000000009;typedef vector<LL> vec;typedef vector<vec> mat;LL n;mat mul(mat &A,mat &B){    mat C(A.size(),vec(B[0].size()));    for( int i = 0; i < A.size(); i++ ){        for( int k = 0; k < B.size();k++ ){            for( int j = 0; j < B[0].size();j++ ){                C[i][j] = (C[i][j] + A[i][k] * B[k][j]);                C[i][j] %= MOD;            }        }    }    return C;}mat pow(mat A,LL n){    mat B(A.size(),vec(A.size()));    for( int i = 0; i < A.size(); i++ ){        B[i][i] = 1;    }    while(n > 0){        if(n & 1)B = mul(B,A);        A = mul(A,A);        n >>= 1;    }    return B;}void solve(){    mat A(2,vec(2));    A[0][0] = A[0][1] = A[1][0] = 1;    A[1][1] = 0;    A = pow(A,n);    printf("%lld\n",A[1][0]);}int main(){    scanf("%lld",&n);        solve();    return 0;}
0 0