1. 程式人生 > >HDU 6198 number number number 矩陣快速冪 找規律

HDU 6198 number number number 矩陣快速冪 找規律

for spa iostream pri amp span ios const isp

  題目鏈接: 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啊 老哥, 所以就是找規律, 自己對數字的敏感程度還是不夠啊, 多練練吧

HDU 6198 number number number 矩陣快速冪 找規律