P2626 斐波那契数列(升级版) 洛谷(2626)

https://www.luogu.org/problem/show?pid=2626

题目背景

大家都知道,斐波那契数列是满足如下性质的一个数列: • 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

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cmath>
 4 
 5 const long long mod=pow(2,31);
 6 
 7 using namespace std;
 8 
 9 long long x,num=2,cnt,n;
10 
11 int main()
12 {
13     cin>>n;
14     double x=sqrt(5.0);
15     cnt=1/x*((pow((1+x)/2,n)-(pow((1-x)/2,n))));
16     cnt%=mod;
17     long long zz=cnt;
18     cout<<cnt<<'=';
19     while(cnt!=1)
20     {
21         while(cnt%num==0)
22         {
23             if(cnt<zz) cout<<'*'<<num;
24             else     cout<<num;
25             cnt/=num;
26         }
27         num++;
28     }
29     return 0;
30 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/6675532.html