标签:数学
题目链接:点击打开链接
解题思路:
上来先把n 分别为1、2、3、4的情况大致列了一下,发现n == 1时结果为2,n== 2时结果为2,n == 3时结果为4,n== 4时结果为6.
于是大胆的猜想ans[i] = ans[i - 1] + ans[i - 2],但是WA在#12。把预处理表打出来看了看······当n达到45时,结果都溢出了,并且此题long long存不下,果断开unsigned long long。
完整代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
int n;
const int maxn = 55;
unsigned long long ans[maxn];
int main()
{
#ifdef DoubleQ
freopen("in.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(0);
ans[1] = ans[2] = 2;
for(int i = 3; i < maxn ; i ++)
ans[i] = ans[i - 1] + ans[i - 2];
while(cin >> n)
{
cout << ans[n] << endl;
}
}
标签:数学
原文地址:http://blog.csdn.net/u013447865/article/details/44081271