1005 number number number

来源:互联网 发布:氰化钠 淘宝 编辑:程序博客网 时间:2024/05/20 16:34

题目:http://acm.hdu.edu.cn/showproblem.php?pid=6198

#include <iostream>#include <cstddef>#include <cstring>#include <vector>using namespace std;typedef long long ll;const int mod=998244353;typedef vector<ll> vec;typedef vector<vec> mat;mat mul(mat &a,mat &b){    mat c(a.size(),vec(b[0].size()));    for(int i=0; i<2; i++)    {        for(int j=0; j<2; j++)        {            for(int k=0; k<2; k++)            {                c[i][j]+=a[i][k]*b[k][j];                c[i][j]%=mod;            }        }    }    return c;}mat pow(mat a,ll n){    mat res(a.size(),vec(a.size()));    for(int i=0; i<a.size(); i++)        res[i][i]=1;//单位矩阵;    while(n)    {        if(n&1) res=mul(res,a);        a=mul(a,a);        n/=2;    }    return res;}ll solve(ll n){    mat a(2,vec(2));    a[0][0]=1;    a[0][1]=1;    a[1][0]=1;    a[1][1]=0;    a=pow(a,n);    return a[0][1];//也可以是a[1][0];}int main(){    ll n;    int k;    while(cin>>n&&n!=-1)    {        k=(5+(n-1)*2);        cout<<(solve(k)-1+998244353)%998244353<<endl;    }    return 0;}


原创粉丝点击