洛谷P1962 斐波那契数列
#include <cstdio> #include <algorithm> #include <cstring> #include <cctype> using namespace std; inline long long read() { long long x=0, f=1; char c; while (!isdigit(c=getchar())) if (c == '-') f=-1; while(isdigit(c)) x=(x<<3)+(x<<1)+(c&15),c=getchar(); return x*f; } inline void write(long long x) { if(x<0) putchar('-'),write(~x+1); else{ if(x > 9) write(x/10); putchar(x%10+'0'); } } const int mod= 1000000007; long long n; struct matrix { long long mat[105][105]; int n, m; matrix() { memset(mat,0,sizeof(mat)); n=0; m=0; } matrix(int val1,int val2) { memset(mat,0,sizeof(mat)); n=val1; m=val2; } matrix operator*(const matrix& rhs)const { matrix ans(n,rhs.m); for (register int i=1; i<=n; i++) for (register int k=1;k<=m; k++) for (register int j=1; j<=rhs.m;j++) ans.mat[i][j]=(ans.mat[i][j]+mat[i][k]*rhs.mat[k][j])%mod; return ans; } matrix operator*=(const matrix& rhs) { return *this=*this*rhs; } }; matrix power(matrix a, long long b) { matrix ans(1,2); ans.mat[1][1]=ans.mat[1][2]=1; while(b) { if (b&1) ans*=a; a*=a; b>>=1; } return ans; } int main() { n=read(); if (n<=2) { write(1); return 0; } matrix base(2,2); base.mat[1][1]=base.mat[1][2]=base.mat[2][1]=1; matrix ans=power(base,n-2); write(ans.mat[1][1]); return 0; }