2017 ACM/ICPC Asia Regional Shenyang Online 1005 number number number (HDU6198)

来源:互联网 发布:2015西安行知中学官网 编辑:程序博客网 时间:2024/06/10 16:31

题目链接:number number number
思路:找规律,当k=1时,n=F5-1=4。k=2,n=F7-1=12。k=3,n=F9-1=33。所以大胆推测n=F(2*k+3)-1,得到序列4、12、33、88、…
递推公式:f(n) = 3*f(n-1) - f(n-2) + 1 用矩阵快速幂求解
构造矩阵:
| 3 1 0|
|-1 0 0| * [f(n) f(n-1) 1] = [f(n+1) f(n) 1]
| 1 0 1|

#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <queue>#include <string>#include <cstring>#include <map>#include <stack>#include <set>#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long ll;int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};const double eps = 1e-6;const double Pi = acos(-1.0);const int INF=0x3f3f3f3f;const int dim = 3;const ll MOD = 998244353;#define mod(x) ((x)%MOD)ll n;struct mat{    ll m[dim][dim];}unit;mat operator * (mat a,mat b){    mat ret;    ll x;    for(ll i = 0;i < dim;i++){        for(ll j = 0;j < dim;j++){            x = 0;            for(ll k = 0;k < dim;k++)                x += mod((ll)a.m[i][k]*b.m[k][j]);            ret.m[i][j] = mod(x);        }    }    return ret;}void init_unit(){    for(ll i = 0;i < dim;i++)        unit.m[i][i] = 1;    return ;}mat pow_mat(mat a,ll n){    mat ret = unit;    while(n){        if(n&1) ret = ret*a;        a = a*a;        n >>= 1;    }    return ret;}int main(){    ll n;    init_unit();    while(~scanf("%lld",&n)){        if(n == 1) printf("4\n");        else if(n == 2) printf("12\n");        else{            mat a,b;            b.m[0][0]=3,b.m[0][1]=1,b.m[0][2]=0;            b.m[1][0]=-1,b.m[1][1]=0,b.m[1][2]=0;            b.m[2][0]=1,b.m[2][1]=0,b.m[2][2]=1;            a.m[0][0]=12,a.m[0][1]=4,a.m[0][2]=1;            b = pow_mat(b,n-2);            a = a*b;            printf("%lld\n",(a.m[0][0]+MOD)%MOD);        }    }    return 0;}
阅读全文
0 0
原创粉丝点击