还有另一种斐波那契数:F(0) = 7,F(1) = 11,F(n) = F(n-1) + F(n-2) (n>=2)。
输入
输入由一系列行组成,每行包含一个整数 n.(n < 1,000,000)。
输出
如果 3 平均除以 F(n),则打印单词“yes”。
如果没有,请打印单词“no”。
样本
输入复制 | 输出复制 |
---|---|
0 1 2 3 4 5 | no no yes no no no |
#include<iostream>
#include<vector>
using namespace std;
#define int long long
const int maxn = 1e6 + 10;
vector<int>F(maxn);
void Init()
{
F[0] = 7, F[1] = 11;
// 7 11 18 29 47 76 123 199 322 521 843
// 0 1 2 3 4 5 6 7 8 9 10
// n n n y n n y n n n y
for (int i = 2; i <= maxn; i++)
{
F[i] = F[i - 1] + F[i - 2];
}
for(int i = 0;i < 100;i++)
{
if(F[i] % 3 == 0)cout << i << " ";
// cout << "第" << i << "个";
// F[i] % 3 ? cout << "no" << endl : cout << "yes" << endl;
}
}
signed main()
{
// Init();
//2 6 10 14 18 22 26
//30 34 38 42 46 50 54
//58 62 66 70 74 78 82
//86 87 88
int N;
while(cin >> N)
{
(N + 2) % 4 ? cout << "no" << endl : cout << "yes" << endl;
}
// while (cin >> N)
// {
// F[N] % 3 ? cout << "no" << endl : cout << "yes" << endl;
// }
return 0;
}
首先,按照一般数组存放再次斐波那契数列的值,再判断输出由于数量级太大必然是炸了(光是到86就已经不行了)参考上列代码输出,可以发现从2开始,每隔4就可以%3一次,那么就通过该规律直接进行输出即可
#include<iostream>
using namespace std;
#define int long long
#define endl '\n'
signed main()
{
int N;
while (cin >> N)
(N + 2) % 4 ? cout << "no" << endl : cout << "yes" << endl;
return 0;
}