【Wannafly挑战赛9-C】列一列(斐波那契)

链接:https://www.nowcoder.net/acm/contest/71/C

题目描述

小W在计算一个数列{An},其中A1=1,A2=2,An+2=An+1+An。尽管他计算非常精准,但很快他就弄混了自己的草稿纸,他找出了一些他计算的结果,但他忘记了这些都是数列中的第几项。

输入描述:

每行包括数列中的一项Ak(k<=100000)。

总行数T<=30。

输出描述:

对于每一项Ak,输出一行包括一个正整数k表示输入中数是数列的第几项。

示例1

输入

2
3
5
8
13

输出

2
3
4
5
6

代码:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int mod = 1000000009;
 5 const int N = 1e5+3;
 6 int a[N];
 7 char s[100006];
 8 map<int, int>M;
 9 int main()
10 {
11     a[1] = 1, M[1] = 1, a[2] = 2, M[2] = 2;
12     for(int i = 3; i <= 1e5; i++)
13     {
14         a[i] = (a[i-1] + a[i-2]) % mod;
15         M[a[i]] = i;
16     }
17     while(~scanf("%s" ,s))
18     {
19         int n = 0;
20         for(int i = 0; s[i]; i++)
21             n = (n * 10LL + s[i] - '0') % mod; //数字+LL就是强制转换
22         printf("%d
", M[n]);
23     }
24     return 0;
25 }
View Code
原文地址:https://www.cnblogs.com/lesroad/p/8432896.html