斐波那契数列的通项公式x+洛谷P2626x
#include<cstdio> #include<iostream> #include<cmath> using namespace std; int main() { int n; scanf("%d",&n); n--; double q=sqrt(5.0); int ans; ans=((pow((1+q)/2.0,n)/q-(pow((1-q)/2.0,n)/n))); cout<<ans<<endl; return 0; }
洛谷P2626
题目背景
大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数)。
题目描述
请你求出第n个斐波那契数列的数mod(或%)2^31之后的值。并把它分解质因数。
输入输出格式
输入格式:
n
输出格式:
把第n个斐波那契数列的数分解质因数。
输入输出样例
输入样例#1:
5
输出样例#1:
5=5
输入样例#2:
6
输出样例#2:
8=2*2*2
说明
n<=48
上代码:
#include <iostream> #include <cstdio> #include <cmath> #define Max 300 const long long Mod = pow (2, 31); void read (long long &now) { now = 0; char word = getchar (); while (word < '0' || word > '9') word = getchar (); while (word >= '0' && word <= '9') { now = now * 10 + word - '0'; word = getchar (); } } long long fibonacii[Max]; int main (int argc, char *argv[]) { register long long N; fibonacii[1] = 1; fibonacii[2] = 1; read (N); for (long long i = 3; i <= N; i++) fibonacii[i] = (fibonacii[i - 1] + fibonacii[i - 2]) % Mod; long long Count = 0; long long number = 2; register long long now = fibonacii[N]; printf ("%lld=", now); while (now != 1) { if (now % number) number++; else { Count++; if (Count == 1) printf ("%lld", number); else printf ("*%lld", number); now /= number; } } return 0; }