hoj2761-Gibonacci Sequence
Gibonacci Sequence
Source : XiaoE | |||
Time limit : 1 sec | Memory limit : 64 M |
Submitted : 284, Accepted : 122
As we know, the Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f(0) and f(1) which are respectively zero and one.
Now the Gibonacci’s definition is like this
g(0) = 0, g(1) = 3, g(n) = g(n - 1) + 2g(n - 2) for n > 1
Perhaps you saw the Gibonacci before, the problem is not the g(k) for some k input, but to calculate the number of digits of g(k). It’s an easy problem, isn’t it?
Input
For each test case, a line will contain an integer k between 0 and 10^8 inclusively.
Output
Calculate the number of digits of the Gibonacci number g(k) and output the result in a single line.
Sample Input
1 4
Sample Output
1 2
地址:http://acm.hit.edu.cn/hoj/problem/view?id=2761
这道题。。。。。。。。。还以为是矩阵快速幂。。。。。却发现它出现在水题列表里,没有想明白。。。。。。。。。
于是。。。。。。
g(n) = g(n - 1) + 2g(n - 2), g(0) = 0, g(1) = 3 特征方程 r2 – r - 2 = 0 特征根 r1 = 2, r2 = -1 则设 g(n) 通项 g(n) = a * 2n + b * (-1)n 代入g(0), g(1) a + b = 0, 2a – b = 3 a = 1, b = -1 g(n) = 2n – (-1)n 求g(n)位数 即求1 + log10g(n) 由于(-1)n不会影响位数, 所以结果即为n*log102 + 1
所以。。。。。。。根本不需要求矩阵,汗。。。。。。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <algorithm> #define LL long long #define M 10 using namespace std; int main() { int a; while(cin>>a){ cout<<(int)(a*log10(2)+1)<<endl; } return 0; }
两行?。。。。。好羞愧。。。。。。。。