HDU 6198 number number number 矩阵快速幂 找规律

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

  题目描述: 给你一个k, 你可以用K个斐波那契数列中的数来构造一个数, 现在我们要求构造不出来的那个最小的数字

  解题思路: 首先我们把斐波那契数列写出来, 0, 1, 1, 2, 3, 5, 8, 13, 21, 43 . . . . . . , 我们首先发现当K == 1 的时候构造不出来的数显然是4, 然后2个的时候是12, 三个是33, 然后找规律就是 f(2*n+3)-1 ..... 说实话这题挺水的.......

  代码: 

//#include <iostream>
//#include <cstdio>
//#include <string>
//#include <vector>
//#include <cstring>
//#include <iterator>
//#include <cmath>
//#include <algorithm>
//#include <stack>
//#include <deque>
//#include <map>
//#include <set>
//#include <queue>
//#define lson l, m, rt<<1
//#define rson m+1, r, rt<<1|1
//#define mem0(a) memset(a,0,sizeof(a))
//#define mem1(a) memset(a,-1,sizeof(a))
//#define sca(x) scanf("%d",&x)
//#define de printf("=======\n")
//typedef long long ll;
//using namespace std;
//
//int main() {
//    ll n, k;
//    while( scanf( "%lld%lld", &n, &k ) == 2 ) {
//        printf( "%lld\n", k * (n-k+1) );
//    }
//    return 0;
//}
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;

const int M = 998244353;

struct Matrix {
    long long a[2][2];
    Matrix() {
        memset(a, 0, sizeof(a));
    }
    Matrix operator * (const Matrix y) {
        Matrix ans;
        for(int i = 0; i <= 1; i++)
            for(int j = 0; j <= 1; j++)
                for(int k = 0; k <= 1; k++)
                    ans.a[i][j] += a[i][k]*y.a[k][j];
        for(int i = 0; i <= 1; i++)
            for(int j = 0; j <= 1; j++)
                ans.a[i][j] %= M;
        return ans;
    }
    void operator = (const Matrix b) {
        for(int i = 0; i <= 1; i++)
            for(int j = 0; j <= 1; j++)
                a[i][j] = b.a[i][j];
    }
};

long long solve(long long x) {
    Matrix ans, trs;
    ans.a[0][0] = ans.a[1][1] = 1;
    trs.a[0][0] = trs.a[1][0] = trs.a[0][1] = 1;
    while(x) {
        if(x&1)
            ans = ans*trs;
        trs = trs*trs;
        x >>= 1;
    }
    return ans.a[0][0];
}

int main() {
    int n;
    while( scanf( "%d", &n ) == 1 ) {
        printf( "%lld\n", solve(2*n+2)-1 );
    }
    return 0;
}
View Code

  思考: 自己一开始想的是递推的....有点儿傻逼了哈...... 但是是1e9啊 老哥, 所以就是找规律, 自己对数字的敏感程度还是不够啊, 多练练吧

posted on 2017-09-10 22:04  FriskyPuppy  阅读(166)  评论(0编辑  收藏  举报

导航