国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

1000. Fibonacci

2019-11-06 06:29:05
字體:
來源:轉載
供稿:網友

1000. Fibonacci 1

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Given an integer n, your goal is to compute the last Fn mod (10^9 + 7).

Input

The input test file will contain a single line containing n (n ≤ 100). There are multiple test cases!

Output

For each test case, PRint the Fn mod (10^9 + 7).

Sample Input

9

Sample Output

34

本題比較簡單,由于最多只需要計算到第100個斐波那契序列。所以直接用long long利用for循環方法得到答案。代碼如下。
#include <iostream>using namespace std;int main(){ int n; while(cin>>n) { long long f0=0, f1=1; if(n==0) cout << 0 << endl; else if(n==1) cout << 1 << endl; else{ long long f2; for(int i=2;i<=n;i++) { f2=f0+f1; f0=f1; f1=f2; } f2=f2%(1000000007); cout << f2 << endl; } } return 0;}

1001. Fibonacci 2

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:   0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …   Given an integer n, your goal is to compute the last Fn mod (10^9 + 7).

Input

 The input test file will contain a single line containing n (n ≤ 2^31-1). There are multiple test cases!    

Output

 For each test case, ple Input 9

Sample Output

34

Hint

 You may need to use “long long”.

#include <cmath>#include <cstring>using namespace std;#define MOD 1000000007struct matrix{ long long a[2][2];};matrix mul(matrix x,matrix y){ matrix m; memset(m.a,0,sizeof(m.a)); for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) m.a[i][j]=(m.a[i][j]+x.a[i][k]*y.a[k][j])%MOD; return m;}void power(int n){ matrix t,result; t.a[0][0]=1; t.a[0][1]=1; t.a[1][0]=1; t.a[1][1]=0; memset(result.a,0,sizeof(result.a)); for(int i=0;i<2;i++) result.a[i][i]=1; while(n) { if(n&1) result=mul(result,t); t=mul(t,t); n=n>>1; } cout << result.a[0][1] << endl;}int main(){ int n; while(cin>>n) { power(n); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁安市| 江川县| 姚安县| 巴南区| 收藏| 中牟县| 白河县| 镇安县| 延川县| 华池县| 长治县| 汕头市| 浦县| 即墨市| 舟山市| 文登市| 夏津县| 志丹县| 保德县| 屯昌县| 环江| 枣阳市| 喀什市| 杭锦旗| 东阳市| 新田县| 大埔县| 乌恰县| 壤塘县| 仁怀市| 汉川市| 浮山县| 黔江区| 天门市| 登封市| 巫溪县| 娄底市| 缙云县| 西城区| 昌黎县| 都安|